Results 1 to 2 of 2
- 05-26-2010, 10:36 PM #1
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Trouble listing elements in an XML
Hi all,
I'm pretty new to this XML parsing. I've got the hang of most of it. However, there is something that bugs me and I can't figure it out. I'm trying to follow this simple tutorial from IBM, where the sample xml file is as follows:
Java Code:<?xml version="1.0"?> <surveys> <response username="bob"> <question subject="appearance">A</question> <question subject="communication">B</question> <question subject="ship">A</question> <question subject="inside">D</question> <question subject="implant">B</question> </response> <response username="sue"> <question subject="appearance">C</question> <question subject="communication">A</question> <question subject="ship">A</question> <question subject="inside">D</question> <question subject="implant">A</question> </response> <response username="carol"> <question subject="appearance">A</question> <question subject="communication">C</question> <question subject="ship">A</question> <question subject="inside">D</question> <question subject="implant">C</question> </response> </surveys>
Java Code:import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.InputSource; import org.xml.sax.helpers.DefaultHandler; public class SAXParser extends DefaultHandler{ public SAXParser() { //System.out.println("Object initiated..."); } @Override public void error (SAXParseException e) { System.out.println("Error parsing the file: "+e.getMessage()); } @Override public void warning (SAXParseException e) { System.out.println("Problem parsing the file: "+e.getMessage()); } @Override public void fatalError (SAXParseException e) { System.out.println("Error parsing the file: "+e.getMessage()); System.out.println("Cannot continue, exiting..."); System.exit(1); } @Override public void startDocument() throws SAXException { System.out.println("Parsing the file..."); } @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { System.out.print("Start element: "); System.out.println(localName); if(localName.equals("surveys")) { // Just trying to see if it finds the element at all System.out.println(" -- Found surveys"); } for (int att = 0; att < atts.getLength(); att++) { String attName = atts.getLocalName(att); System.out.println(" " + attName + ": " + atts.getValue(attName)); } } public static void main(String[] args) { XMLReader saxReader = null; try { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setValidating(false); SAXParser saxParser = parserFactory.newSAXParser(); saxReader = saxParser.getXMLReader(); saxReader.setContentHandler( new SAXParser() ); saxReader.setErrorHandler( new SAXParser() ); InputSource xmlfile = new InputSource( args[0] ); saxReader.parse(xmlfile); } catch (Exception e) { System.err.println(e); System.exit(1); } } }
Java Code:... Start element: Start element: username: bob Start element: subject: appearance Start element: subject: communication Start element: subject: ship Start element: ...
Java Code:Start element: surveys Start element: response username: bob ...
Any ideas?
Thanks!
Best regards
/T
- 05-27-2010, 10:06 AM #2
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Ok, I got it all solved in another forum and it turned out that I used the wrong variable. If I interpret the API description correctly, localName is used when using Namespace processing (which I don't?):
localName - The local name (without prefix), or the empty string if Namespace processing is not being performed.
qName - The qualified name (with prefix), or the empty string if qualified names are not available.
Thought I'd let you know.
Thanks!
/T
Similar Threads
-
Listing Installed DSA
By anoopasta in forum XMLReplies: 1Last Post: 05-06-2010, 05:10 PM -
Listing tables of a database
By Java Tip in forum Java TipReplies: 0Last Post: 02-12-2008, 10:30 AM -
Listing subdirectories/files
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 08:19 AM -
Listing all available Locales
By Java Tip in forum Java TipReplies: 0Last Post: 12-29-2007, 05:54 PM
Bookmarks