0

I am new with this, so why is that when I want to print the value for the NAME element in StartElement(), for all 3 elements it prints null ?

class Test {
  public static void main(String args[]) {
    try {
      SAXParserFactory factory = SAXParserFactory.newInstance();
      SAXParser saxParser = factory.newSAXParser();
      DefaultHandler handler = new DefaultHandler() {


        public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {
          if (qName.equals("NAME")) {
          String str = attributes.getValue("NAME");
           System.out.println(str);

          }
        }  
      };

      saxParser.parse(new InputSource(new StringReader(xmlString)), handler);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  static String xmlString = "<PHONEBOOK>" +
      "  <PERSON>" +
      "   <NAME>Joe Wang</NAME>" +
      "   <EMAIL>[email protected]</EMAIL>" +
      "   <TELEPHONE>202-999-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  <PERSON>   " +
      "<NAME>Karol</NAME>" +
      "   <EMAIL>[email protected]</EMAIL>" +
      "   <TELEPHONE>306-999-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  <PERSON>" +
      "   <NAME>Green</NAME>" +
      "   <EMAIL>[email protected]</EMAIL>" +
      "   <TELEPHONE>202-414-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  </PHONEBOOK>";
}

2 Answers 2

1

Your XML doesn't have any attributes. So this:

String str = attributes.getValue("NAME");
System.out.println(str);

... is never going to print anything out.

You want to print out the content of the NAME element, by the looks of it. So you'd want to handle the characters event when you're in the NAME element.

Do you have to use SAX, by the way? In my experience it's a pain in the neck compared with using a DOM-like model (e.g. using JDOM or another API - the built-in one is somewhat painful).


An alternative approach if you're in control of the XML is to start using attributes:

<PERSON NAME="Joe Wang"
        EMAIL="[email protected]"
        TELEPHONE="202-999-9999"
        WEB="www.java2s.com" />

Then you can use attributes.getValue("NAME") when you get a PERSON element.

Sign up to request clarification or add additional context in comments.

3 Comments

Well the project that I am working at uses SAX, thats why i made this litlle test to understand how it works.
@vBx: And is that for a good reason (like it handling huge files)? Or could it be reasonably changed? You may want to at least consider it. Obviously it's not impossible to use SAX, but it's just a pain in terms of maintaining the right amount of context.
well this is how i found the project(at work) so i don't have any experince in this, but after I gain some, I will surelly change it, I read that SAXPArsers are good only for big files and thats it
1

That's because you're trying to read the attribute from the <NAME> element (which doesn't have any attribute).

String str = attributes.getValue("NAME");

To read the content of the <NAME> element, you will have to override the characters (char ch[], int start, int length) method of DefaultHandler and display all the ch[] (character array) returned.

3 Comments

So how can I get value of the NAME element then ?
@vBx, I said it already, override the characters method from the DefaultHandler` class.
Hm, how did I missed that ? :) Sorry

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.