akshay
akshay

Reputation: 5285

problem in parsing a xml file

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?

Upvotes: 1

Views: 613

Answers (4)

Vineet Reynolds
Vineet Reynolds

Reputation: 76719

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.

Upvotes: 1

YODA
YODA

Reputation: 309

Maybe you should try this:

c.getTextContent()

Upvotes: 0

Crollster
Crollster

Reputation: 2771

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.

Upvotes: 2

wjans
wjans

Reputation: 10115

Try changing this:

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

into:

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

Upvotes: 1

Related Questions