【发布时间】:2012-07-17 17:46:42
【问题描述】:
我需要通过 id 递归查找 xml 节点,如下所示:
<categories>
<category id="1">
</category>
<category id="2">
<category id="3">
</category>
<category id="4">
<category id="5">
</category>
</category>
</category>
</categories>
我正在使用 DocBuilder。
我知道有 getElementById() 方法,但它在我的情况下不起作用,比如说,当我在根节点中时,我想找到一个带有 id="5" 的元素。
有可能吗?
【问题讨论】:
-
getElementById()搜索整个文档,而不仅仅是元素的子节点。如果您有一个指定属性为xml:id的模式,它应该做您想做的事。 (见这个问题:stackoverflow.com/questions/3423430/…)