gpt4 book ai didi

Java XML getElementsByTagName() 函数

转载 作者:数据小太阳 更新时间:2023-10-29 02:05:38 27 4
gpt4 key购买 nike

假设我有这个 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() 解决了我的问题。对于给您带来的不便,我们深表歉意。

最佳答案

Dom Core 2 Spec说:

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.

Dom Core 3 Spec说:

getElementsByTagName

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

所以您对该功能的期望是正确的。如果这不是您得到的结果,那将是您的代码或您正在使用的库中的错误。

关于Java XML getElementsByTagName() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5614460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com