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;


        }
        }
}
4
  • 2
    getTextValue()? It'd be easier if you'd posted some code. Commented Nov 16, 2011 at 15:13
  • Thats not a valid method, i have added code, Commented Nov 16, 2011 at 15:19
  • getTextContent()? (mis-typed) But why are you skipping text nodes? Commented Nov 16, 2011 at 15:20
  • Gives; Exception in thread "main" java.lang.AbstractMethodError: org.apache.xerces.dom.DeferredTextImpl.getTextContent()Ljava/lang/String; TEXT_NODE is blank because everything i try returns null Commented Nov 16, 2011 at 15:22

1 Answer 1

1

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.

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

3 Comments

How would i go about missing out the car element and its text value? i think this is where i went wrong!
@Lunar I'm not sure what you mean; are you trying specifically to get just the car element and its value?
@Lunar Awesome, glad you worked it out! XML can be a pain; I use XPath-ish stuff almost exclusively now.

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.