简体   繁体   English

Java中的XML文档遍历

[英]XML Document traversal in java

which is the most preferable xml document traversal method in java ? 哪一个是Java中最可取的xml文档遍历方法? Using getElementsByTagName or using TreeWalker . 使用getElementsByTagName或使用TreeWalker

I've one TreeModel . 我有一个TreeModel a Dom Node is the root of the TreeModel . Dom NodeTreeModel的根。 There are two Thread s adding nodes to it. 有两个Thread向其添加节点。 One Thread is adding nodes according the nodes added by the other Thread . 一个Thread正在根据另一Thread添加的节点添加节点。

eg 例如

One Thread adding Node s named App . 一个Thread添加名为App Node The other Thread adding nodes according to the name attribute of the Node s named App . 另一个Thread根据名为AppNodename属性添加节点。 Sometimes the nodes are not added correctly. 有时,节点未正确添加。 The TreeModel only shows the details in the elements by traversing through the nodes. TreeModel仅通过遍历节点来显示元素中的详细信息。

Note: Adding the App Node is according to the Name attribute of the Node . 注意:根据Node的Name属性添加App Node

Currently for the second Thread , the Node s are taken by calling getElementsByTagName . 当前,对于第二个Thread ,通过调用getElementsByTagName获取Node Is there any advantage by changing it to TreeWalker ? 将其更改为TreeWalker有什么好处吗?

I like XPath. 我喜欢XPath。 W3schools link here , Javadocs here . W3schools在这里链接Javadocs在这里 It is tedious to get started with factories and builders, IMO write your own utility class to save on that tedium. 从工厂和建筑商开始是一件很乏味的事情,IMO编写了自己的实用程序类来节省繁琐的工作。 But the syntax to traverse around is expressive and powerful, and it is a "standard" with good documentation. 但是,遍历的语法表达力强,功能强大,并且是具有良好文档说明的“标准”。

If you are brave, check out my beta Groovy-like xpath-like project , but I would not propose this as "the most preferable". 如果您很勇敢,请查看我的Beta版Groovy式xpath式项目 ,但我不建议将此作为“最推荐的”。 :-) :-)

ADDED : XPath is a query language for selecting nodes from an XML document. 添加 :XPath是一种查询语言,用于从XML文档中选择节点。 It is good for traversing (moving around in) a DOM structure. 这对于遍历 (到处移动)DOM结构非常有用。 However, OP's updated requirements are for manipulating / modifying the DOM structure. 但是,OP的更新要求是用于操纵/修改 DOM结构。 XPath is not a good fit there. XPath不太适合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM