Java XML getElementsByTagName() 函数
假设我有这个 XML 文件:
<attributes>
<units>
<civilians>
<unit>
<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>
节点属性上的 getElementsByTagName("attack") 是否返回一个 NodeList,其第一个位置包含 26 的攻击元素和第二个位置包含 33 的攻击元素?
我一直在想是这样的,但似乎行不通。
如果事情不是这么简单;从 XML 文件中获取所有攻击值的好方法是什么?也许 XML 文件本身的结构很糟糕?
编辑:啊。我现在得到了节点,并且 .getTextContent() 而不是 .getNodeValue() 解决了我的问题。很抱歉给您带来不便。
Lets say i have this XML file:
<attributes>
<units>
<civilians>
<unit>
<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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Dom Core 2 规格 说:
Dom Core 3 Spec 说:
所以你对该功能的期望是正确的。如果这不是您所得到的,则可能是您的代码或您正在使用的库中的错误。
Dom Core 2 Spec says:
And Dom Core 3 Spec says:
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.