Results 1 to 2 of 2
  1. #1
    circle is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default 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>
    And I've assembled the following code, which seems to run, but it never reports any elements as clearly stated in the tutorial (Pretty much the same code used as well).

    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);          
            }
        }
    }
    The output is like follows:

    Java Code:
    ...
    Start element:
    Start element:
     username: bob
    Start element:
     subject: appearance
    Start element:
     subject: communication
    Start element:
     subject: ship
    Start element:
    ...
    It should say:

    Java Code:
    Start element: surveys
    Start element: response
     username: bob
    ...
    Either I'm doing something wrong in the ParserFactory or in the startElement part

    Any ideas?

    Thanks!

    Best regards
    /T

  2. #2
    circle is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    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.
    Exchanging localName for qName did the trick! *smacking forehead*

    Thought I'd let you know.
    Thanks!

    /T

Similar Threads

  1. Listing Installed DSA
    By anoopasta in forum XML
    Replies: 1
    Last Post: 05-06-2010, 05:10 PM
  2. Listing tables of a database
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-12-2008, 10:30 AM
  3. Listing subdirectories/files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:19 AM
  4. Listing all available Locales
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 05:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •