1

I am using following code to get values of a particular tag in xml:

DocumentBuilderFactory docBuilderFactory  =  DocumentBuilderFactory.newInstance();
Document doc = null;
DocumentBuilder docbuilder = docBuilderFactory.newDocumentBuilder();
doc = docbuilder.parse(jobFilePath);
NodeList nodeList = doc.getElementsByTagName("xyz");
System.out.println("nodelist Lengthhhhhhhhhh = " + nodeList.getLength());

for (int i=0;i<nodeList.getLength();i++) {
    Node c= nodeList.item(i);
    System.out.println("val =" + c.getNodeValue());
}

input xml is

<abc> 
        <xyz p="Layouts">a</xyz>
        <xyz p="Layouts">b</xyz>
        <xyz p="Layouts">3</xyz>
</abc>

Output is

nodelist Lengthhhhhhhhhh 3
val = null
val = null
val = null

why is it coming out null?

1
  • i wan to fetch values of tag xyx , op should be a b 3 Commented Jun 21, 2011 at 4:31

4 Answers 4

2

Going out on a limb here... but from my memories of DOM manipulation in Javascript, you need to use:

 Node c= nodeList.item(i).getFirstChild();

As the firstChild contains the actual value of the tag itself.

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

Comments

1

Try changing this:

System.out.println("val =" + c.getNodeValue());

into:

System.out.println("val =" + c.getTextContent());

2 Comments

c.getTextContent()) worked ! but i wonder why c.getNodeValue is not working?
c references the whole node <xyz p="Layouts">a</xyz> that has no node value (see javadoc for more details). You could actually get the first (and only) child of that node as well and get that one's node value, that would be a text node containing a/b/3. But since you know what your element looks like, you can directly call getTextContent() on your node.
1

Like others have stated getTextContent() will provide you with the contents of the Text nodes of the xyz elements.

The actual reason for this behavior of getNodeValue() that you've asked for, is specified in the DOM Level 2 Core API:

The attributes nodeName, nodeValue and attributes are included as a mechanism to get at node information without casting down to the specific derived interface. In cases where there is no obvious mapping of these attributes for a specific nodeType (e.g., nodeValue for an Element or attributes for a Comment), this returns null.

When the line Node c= nodeList.item(i); is executed, the Node c contains an object of type Element (with nodeName=xyz, nodeValue=null). It is the child Text node of these xyz elements that actually contain the values a, b and 3. The method getTextContent() returns the value of the text nodes of given element, where as getNodeValue() just returns the nodeValue attribute as applicable to a particular node.

The DOM Level 2 Core API also specifies what these values are, for the other types of nodes in the Document. Note that the nodeValue for text nodes happens to be the content of the node.

Comments

0

Maybe you should try this:

c.getTextContent()

Comments

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.