Java: Child Node values are null

Viewed 1489

My xml (created by calling a Powershell command through Java):

<?xml version="1.0" encoding="UTF-8"?>
-<Objects>
    -<Object>
        <Property Name="DisplayName">Adobe AIR</Property>
        <Property Name="DisplayVersion">27.0.0.124</Property>
        <Property Name="Publisher">Adobe Systems Incorporated</Property>
        <Property Name="InstallDate"/>
    </Object>

I am trying to the DisplayName, DisplayVersion and Publisher values into variables, but am getting nulls and #text values returned by:

try {

        DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
        DocumentBuilder dB = dBF.newDocumentBuilder();
        Document doc = dB.parse(new File(filename));

        NodeList nodeList = doc.getElementsByTagName("Object");

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);

            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element)node;
                System.out.println("Node " + count.toString());

                NodeList childNodeList = e.getChildNodes();
                for (int j = 0; j < childNodeList.getLength() ; j++) {
                    Node childNode = childNodeList.item(j);
                    System.out.print("Child Node Type: " + childNode.getNodeType() + " - ");
                    System.out.print("Child Element Name: " + childNode.getNodeName() + " - ");
                    System.out.println("Element Value: " + childNode.getNodeValue());
                }
                count++;    

            }
        }

This code returns this output for all 123 nodes that exist in my XML file:

Node 0
Child Node Type: 3 - Child Element Name: #text - Element Value: 

Child Node Type: 1 - Child Element Name: Property - Element Value: null
Child Node Type: 3 - Child Element Name: #text - Element Value: 

Child Node Type: 1 - Child Element Name: Property - Element Value: null
Child Node Type: 3 - Child Element Name: #text - Element Value: 

Child Node Type: 1 - Child Element Name: Property - Element Value: null
Child Node Type: 3 - Child Element Name: #text - Element Value: 

Child Node Type: 1 - Child Element Name: Property - Element Value: null
Child Node Type: 3 - Child Element Name: #text - Element Value: 

I've been searching high and low for examples of how to read an XML file with content formatted like mine and am really struggling with my very limited Java knowledge. Any help appreciated.

1 Answers

You did not go deep enough.

You are displaying only the child nodes themselves, and the whitespace text nodes in between them, without actually looking at the text nodes inside the child nodes -- i.e. the actual text nodes you are interested in.

In your inner loop, for each element node you must retrieve that node's child nodes, among which you will find the text you are seeking

            for (int j = 0; j < childNodeList.getLength() ; j++) {
                Node childNode = childNodeList.item(j);
                NodeList subList = childNode.getChildNodes();
                for (int k = 0; k<subList.getLength(); k++)
                {
                     etc...
                }
            }
Related