Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing xml in java - getTextContent() and getNodeValue() only return \n, \t, and whitespace

Tags:

java

xml

I'm having a little trouble with parsing some XML in java. My XML file is read correctly and I'm able to obtain most information from the file without any trouble (ie. the StreamType node, shown in the xml snippet), using the nodes' getTextContent() function.

BUT, when I try to work with children of a node, both getNodeValue() and getTextContent() return this random value: "\n \t\t".

The NodeList propertyNodes seems to be correctly populated (contains all 18 "Property" elements).

Here's a snippet from my code:

Document document;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(appXML);

...

String typeName = document.getElementsByTagName("StreamType").item(0).getTextContent();

...

String limit = "-1";
NodeList propertyNodes = document.getElementsByTagName("Property");
int nodelistlength = propertyNodes.getLength();
for (int i = 0; i < nodelistlength; i++) {
    Node currentNode = propertyNodes.item(i);
    Node nameNode = currentNode.getFirstChild();
    Node valueNode = currentNode.getLastChild();

    String name = nameNode.getNodeValue();
    String value = valueNode.getNodeValue();

    if (nameNode.getTextContent().equalsIgnoreCase("maxConnections"))
        limit = valueNode.getTextContent();
}

And here's some snips from the XML I'm trying to parse:

<Root>
 <Application> 
  <Streams>
   <StreamType>live</StreamType>
   ...
  </Streams>

  ...

  <Properties> 

   ...

   <Property>
    <Name>maxConnections</Name> 
    <Value>1000</Value> 
   </Property> 

   ...

  </Properties>
 </Application>
</Root>

Any idea what I might be doing wrong here? Thanks very much!

EDIT: Works now, thanks to the tutorial posted by @home. This is how I fixed the code:

1) Modified the block of code starting with "String limit" and ending with the for loop's end bracket:

String limit = "-1";
NodeList propertyNodes = document.getElementsByTagName("Property");
for (int i = 0; i < propertyNodes.getLength(); i++) {
    Node currentNode = propertyNodes.item(i);

    if (currentNode.getNodeType() != Node.ELEMENT_NODE)
        continue;

    Element currentElement = (Element)currentNode;

    if (getTagValue("Name",currentElement).equalsIgnoreCase("maxConnections"))
        limit = getTagValue("Value",currentElement);
}

2) Added this handy function from the tutorial:

private static String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
    Node nValue = (Node) nlList.item(0);
    return nValue.getNodeValue();
}

It fetches the child name/value pairs of the "Properties" element flawlessly now. Thanks very much!

like image 361
Katherine Williams Avatar asked Sep 12 '11 19:09

Katherine Williams


1 Answers

Sadly, the Java DOM is not that easy to use. You must distinguish between different node types. Not the best tutorial, but this is what I just found using Google: http://mkyong.com/java/how-to-read-xml-file-in-java-dom-parser

like image 69
home Avatar answered Oct 20 '22 04:10

home