开发者

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

开发者 https://www.devze.com 2023-04-04 03:38 出处:网络
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 t

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 c开发者_如何学Pythonode 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!


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


That is the correct content of the node’s value.

<a>
    <b>
        stuff
    </b>
</a>

The value of the a node (which in this case equals the text content) does correctly consist of the whitespace between the start and the end tag, omitting of course the whitespace in the b tag because that, well, belongs to the b tag and not to a.

0

精彩评论

暂无评论...
验证码 换一张
取 消