开发者

Noob Question on NodeLists and NodeMaps

I am trying to pull elements out of a parsed XML file by searching a specific node from a NodeList. This is the following code I am using to iterate through multiple parents:

Node node;
NodeList nodeList = document.getElementsByTagName("SomeTag");
for (int i = 0; i < nodeList.getLength(); i++)
{
     NamedNodeMap map = nodeList.item(i).getAttributes();
     node = map.getNamedItem("AnotherTag")
}

However, my node always returns null. When doing some debugging, it appears it has all the values:

Noob Question on NodeLists and NodeMaps

开发者_如何学JAVA

I think it has to do something with the "this$0" reference to itself before I get to the values, because if i do a getLength of the NodeMap, it returns 0. Any thought on how to fix this? Thanks a bunch everyone!


You are accessing the attributes of an element, not its sub-nodes by calling nodeList.item(i).getAttributes();


Have you maybe thought about using JAXB?

You could create Java objects and have JAXB load the XML file and walk through the tree in that way. You can even create the Java objects automatically from the XSD, if you have one.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜