开发者

Java XML getElementsByTagName() function

Lets say i have this XML file:

<attributes>
  <units>
    <civilians>
      <unit>
    开发者_JAVA百科    <name>Settler</name>
        <stats>
          <attack>26</attack>
          <defence>7</defence>
        </stats>
        <costs>
          <lumber/>
          <iron/>
        </costs>
      </unit>
      <unit>
        <name>Infantry</name>
        <stats>
          <attack>33</attack>
          <defence>7</defence>
        </stats>
        <costs>
          <lumber/>
          <iron/>
        </costs>
      </unit>
    </civilians>
  </units>
</attributes>

Does getElementsByTagName("attack") on the node attributes return a NodeList with the attack element containing 26 at first position and the attack element containing 33 at second position?

I've been thinking this is the case but it doesn't seem to work.

If it isn't as simple as this; what is a good way to grab all attack values from the XML file? Maybe the XML file itself is badly structured?

Edit: Ah. I get the nodes now, and .getTextContent() rather than .getNodeValue() solved my problems. Sorry for the inconvenience.


Dom Core 2 Spec says:

getElementsByTagName

Returns a NodeList of all descendant Elements with a given tag name, in the order in which they are encountered in a preorder traversal of this Element tree.

And Dom Core 3 Spec says:

getElementsByTagName

Returns a NodeList of all descendant Elements with a given tag name, in document order.

So your expectations for the function are correct. If that's not what you're getting it would be a bug in your code or in the library you're using.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜