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 6841839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:01:00+00:00 2026-05-27T00:01:00+00:00

I am trying to parse a XML document using Xerces, but I cant seem

  • 0

I am trying to parse a XML document using Xerces, but I cant seem to access the data within the elements, below is a sample XML document;

<sample>
<block>
    <name>tom</name>
    <age>44</age>
    <car>BMW</car>
</block>
<block>
    <name>Jenny</name>
    <age>23</age>
    <car>Ford</car>
</block>
</sample>

SO far the only output I can produce is;

Sample
    block
      name
        age
          car
    block
      name
        age
          car

Which is just a list of the node names. I have tried node.getValue(), but this just returns null, so im guessing thats wrong!

How can I access the data inside? Here is what is the basics so far;

public static void display(String file) {
    try{
        DOMParser parser = new DOMParser();
        parser.parse(file);
        Document doc = parser.getDocument();
        read(doc);
    }
        catch(Exception e){e.printStackTrace(System.err);}
}


public static void read(Node node) {
    if(node == null) {return;}
        int type = node.getNodeType();
        //System.out.print((node));
        switch (type) {
        case Node.DOCUMENT_NODE: {
            display_all(((Document)node).getDocumentElement());
            break;
        }

         case Node.TEXT_NODE:

          break;
        case Node.ELEMENT_NODE: {

            System.out.println(node.getNodeName());

            NodeList child = node.getChildNodes();
            if(child != null) {
                int length = child.getLength();
                for (int i = 0; i < length ; i++) {
                        display_all(child.item(i));
                }
        }

        break;


        }
        }
}
  • 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-27T00:01:01+00:00Added an answer on May 27, 2026 at 12:01 am

    getNodeValue() returns the value of a text node, which you currently skip over.

     public static void read(Node node) {
        if (node == null) {
            return;
        }
    
        int type = node.getNodeType();
        switch (type) {
        case Node.DOCUMENT_NODE: {
            System.out.println("Doc node; name: " + node.getNodeName());
            read(((Document) node).getDocumentElement());
            break;
        }
    
        case Node.TEXT_NODE:
            System.out.println("Text node; value: " + node.getNodeValue().replaceAll("\\s", ""));
            break;
    
        case Node.ELEMENT_NODE: {
            System.out.println("Element node; name: " + node.getNodeName());
            NodeList children = node.getChildNodes();
            int length = children.getLength();
            for (int i = 0; i < length; i++) {
                read(children.item(i));
            }
            break;
        }
        }
    }
    

    I think where you might be getting confused is how XML is actually structured, and what the children of something like this is:

    <element>
      <child_element>foo</child_element>
    </element>
    

    The above code snippet may help explain.

    It’s also why things like dom4j, JAXB, XPath, etc. make things much easier.

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

Sidebar

Related Questions

Hi guys I'm trying to parse an xml document using jquery but i keep
So I'm trying to parse the following XML document with C#, using System.XML: <root
I was trying to parse mathml document using JScience but was not succeed. Following
I'm trying to parse some XML data using jQuery, and as it stands I
I've been trying to write a PHP script to parse an XML document using
I am trying to parse a xml document using NSXMLParser. Every time it result
I'm trying to parse a xml document to get some data for my program.
I'm trying to parse the xml document returned from this link but I get
I'm trying to parse a XML using Apache Commons JXPath. But for some reason,
I am trying to parse xml data using XDocument.Parse wchich throws NotSupportedException, just like

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.