Vincer-Dom and Modern XML/DOM Manipulation
How to manipulate an XML DOM programmatically with a truly fluent API.
Join the DZone community and get the full member experience.Join For Free
Writing or editing DOM-XML in Java (or another language) has never been natural or straightforward. The code structure diverges inexorably from the underlying tree structure it manipulates, obscuring the original intentions.
To give a concrete idea, the following code:
- Reads a pom.xml file (a Maven XML file that most of Java developers know)
- Adds 2 dependencies
- Removes all dependencies having test scope
- Modifies distribution repository, creating potential missing elements.
The complete Dom manipulation has been expressed in a single chained statement, reflecting the tree structure of the manipulated data.
If you don't know about Parent-Chaining Pattern,
.__ returns the parent of the current element. This pattern really shines when manipulating tree like structures.
Using JDom (a library embracing method chaining though), to achieve exactly the same, the best we can do is:
As you can see, Vincer-Dom saves a lot of coding effort while getting code much more readable.
Moreover, the API is very thin as it consists of only 2 classes :
VElement, each one wrapping its
Hope this article makes you attract to use this library and gives you new fresh ideas to design your next APIs.
Opinions expressed by DZone contributors are their own.