Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6071793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:05:16+00:00 2026-05-23T10:05:16+00:00

I am new with this, so why is that when I want to print

  • 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>joe@yourserver.com</EMAIL>" +
      "   <TELEPHONE>202-999-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  <PERSON>   " +
      "<NAME>Karol</NAME>" +
      "   <EMAIL>karol@yourserver.com</EMAIL>" +
      "   <TELEPHONE>306-999-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  <PERSON>" +
      "   <NAME>Green</NAME>" +
      "   <EMAIL>green@yourserver.com</EMAIL>" +
      "   <TELEPHONE>202-414-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  </PHONEBOOK>";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:05:17+00:00Added an answer on May 23, 2026 at 10:05 am

    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="joe@yourserver.com"
            TELEPHONE="202-999-9999"
            WEB="www.java2s.com" />
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello i am little new to all this but i want to know that:
I found out this morning that proc.new works in a class initialize method, but
How can I ensure that this does NOT open in a new window?? onclick=ob=this.form.table;window.open(ob.options[ob.selectedIndex].value)/>
I'm so new to this that I can't even phrase the question right... Anyway,
I am new for this so that asking these questions.. How can I generate
Started working on a new application this week that is running the latest rails
My project doesn't seem to be aware of this new constant that's apparently auto-generated
I'm extremely new to JS and have this code that I'm trying to tweak.
I have this code that saves a pdf file. FileStream fs = new FileStream(SaveLocation,
I have a very simple (new to this) RESTful WCF service that uses a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.