Perl XML:DOM - 让子级处于特定深度
我正在使用 Perl 进行一些 XML 解析,并决定使用 XML::DOM。假设我正在解析包含以下内容的文件:
<document>
<A>
<B/>
<B/>
</A>
<B/>
</document>
“B”元素根据其在文档中的相对位置进行不同的处理(即父级为 A 的 B 与父级为文档的 B 不同)。从对文档节点的引用,是否可以获取作为直接子节点的 B。然后,获取对 A 节点的引用以仅获取其子 B 节点?
谢谢,
Andrew
I'm doing some XML parsing using Perl and decided on XML::DOM. Assume I'm parsing a file containing the following:
<document>
<A>
<B/>
<B/>
</A>
<B/>
</document>
The "B" element is processed differently depending on its relative location in the document (i.e. a B whose parent is A is different from a B whose parent is document). From a reference to to the document node, is it possible to get the B's that are immediate children. Then later, get a reference to the A node to get only the its child B's?
Thanks,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一个想法是加载 XML::DOM::XPath,这使得这些类型的查询非常自然,而且不那么冗长。
作为文档元素子级的 B 节点:
作为文档子级 A 节点的子级的 B 节点:
作为 A 节点在任何位置出现的直接子级的 B 节点:
作为任何 A 节点的后代的 B 节点:
B 节点它们和文档之间有任何一个(恰好一个)祖先:
还有更多! (我在那里添加了很多选项,因为从您的问题中不清楚哪些选项最适合您的问题)。
由于所有值都只是添加了一些方法的普通 XML::DOM 对象,因此您可以毫不费力地与任何现有 XML::DOM 代码混合和匹配。
Another idea would be to load up XML::DOM::XPath, which makes these sorts of queries pretty natural, and way less verbose.
B nodes that are children of the document element:
B nodes that are children of A nodes that are children of the document:
B nodes that are immediate children of A nodes that occur anywhere:
B nodes that are descendants of any A node:
B nodes that have any one (exactly one) ancestor between them and the document:
And a whole lot more! (I threw a lot of options in there because it's not clear from your question which ones are exactly the options that are best suited to your problem).
Since all of the values are just ordinary XML::DOM objects with some methods added, you can mix and match with any existing XML::DOM code with nearly no fuss.
假设您知道所有 B 元素都将是文档或 A 的子元素,则可以使用可选的递归参数 getElementsByTagName。传递 0 意味着仅返回直接子元素:
Assuming that you know that all B elements will be children of either the document or an A, you can use the optional recurse parameter to getElementsByTagName. Passing 0 means to return only direct child elements: