0

I saw an example in mykong - http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/ I tried to make it work for the xml file (below) by making the following modifications to the code in the above page -

1 - Have only two if blocks in startElement() and characters() methods.
2 - Change the print statements in above methods, ie 
    FIRSTNAME and First Name = passenger id
    LASTNAME and Last Name = name        

The problem is - In the output, I see the word passenger instead of the value of passenger id. How do i fix that ?

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:android="www.google.com">

<passenger id="001">
<name>Tom Cruise</name>
</passenger>

<passenger id="002">
<name>Tom Hanks</name>
</passenger>

</root>

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.helpers.DefaultHandler;

public class ReadXMLFileSAX{

public static void main(String argv[]) {

try {

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {

boolean bfname = false;
boolean blname = false;

public void startElement(String uri, String localName,String qName, 
            Attributes attributes) throws SAXException {

    System.out.println("Start Element :" + qName);

    if (qName.equalsIgnoreCase("passenger id")) {
        bfname = true;
    }

    if (qName.equalsIgnoreCase("name")) {
        blname = true;
    }

}

public void endElement(String uri, String localName,
    String qName) throws SAXException {

    System.out.println("End Element :" + qName);

}

public void characters(char ch[], int start, int length) throws SAXException {

    if (bfname) {
        System.out.println("passenger id : " + new String(ch, start,   length));
        bfname = false;
    }

    if (blname) {
        System.out.println("name : " + new String(ch, start, length));
        blname = false;
    }

}

 };

   saxParser.parse("c:\\flight.xml", handler);

 } catch (Exception e) {
   e.printStackTrace();
   }

   }

}
3
  • Can we see your code, pls ? Commented Dec 10, 2012 at 9:24
  • @BrianAgnew - added the code now. Please let me know if it anything else is required. Thanks Commented Dec 10, 2012 at 9:39
  • The answer is here - stackoverflow.com/questions/13818129/… Commented Dec 14, 2012 at 8:00

1 Answer 1

1

In the startElement, when it's for "passenger", Attributes argument you get will have that value.

public void startElement(String uri, String localName,
    String qName, Attributes attributes)
        throws SAXException {
    if (qName.equalsIgnoreCase("passenger") && attributes != null){
        System.out.println(attributes.getValue("id"));
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Actually I don't understand the main code properly. I modified my code (below), but i still can't see passenger id. >>>>>>>>>>>>>>>>>>>>>>>>>>public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException { System.out.println("Start Element :" + qName); if (localName.equalsIgnoreCase("passenger id")) { System.out.println("Passenger: " + attributes.getValue("id")); } if (qName.equalsIgnoreCase("passenger id")) { bfname = true; } if (qName.equalsIgnoreCase("name")) { blname = true; } }
I have updated the code to use qName in startElement. It compares and prints the passenger id.
I changed localName to qName, but i get the same problem - there is no id, only passenger word is printed.
What implementation of SAXParserFactory and SAXParser you're using? It works clean with my default implementation coming with Java 7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.