简体   繁体   中英

XML Document traversal in java

which is the most preferable xml document traversal method in java ? Using getElementsByTagName or using TreeWalker .

I've one TreeModel . a Dom Node is the root of the TreeModel . There are two Thread s adding nodes to it. One Thread is adding nodes according the nodes added by the other Thread .

eg

One Thread adding Node s named App . The other Thread adding nodes according to the name attribute of the Node s named App . Sometimes the nodes are not added correctly. The TreeModel only shows the details in the elements by traversing through the nodes.

Note: Adding the App Node is according to the Name attribute of the Node .

Currently for the second Thread , the Node s are taken by calling getElementsByTagName . Is there any advantage by changing it to TreeWalker ?

I like XPath. W3schools link here , Javadocs here . It is tedious to get started with factories and builders, IMO write your own utility class to save on that tedium. 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". :-)

ADDED : XPath is a query language for selecting nodes from an XML document. It is good for traversing (moving around in) a DOM structure. However, OP's updated requirements are for manipulating / modifying the DOM structure. XPath is not a good fit there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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