Good Java XML DOM Utility

I write the same detailed DOM manipulation code again and again:

Element e1 = document.createElement("some-name"); e1.setAttribute("attr1", "val1"); e2.setAttribute("attr2", "val2"); document.appendChild(e1); Element e2 = document.createElement("some-other-name"); e.appendChild(e2); // Etc, the same for attributes and finding the nodes again: Element e3 = (Element) document.getElementsByTagName("some-other-name").item(0); 

Now I do not want to change the architecture together, i.e. I do not want to use JDOM, JAXB or anything else. Just Java org.w3c.dom . The reasons for this are

  • It's about the old and big old system
  • XML is used in many places, and XSLT is converted several times to get XML, HTML, PDF file.
  • I'm just looking for convenience, not big changes.

I'm just wondering if there is a good wrapper library (e.g. with apache commons or google) that allows me to do similar things using a free style like jRTF :

 // create a wrapper around my DOM document and manipulate it: // like in jRTF, this code would make use of static imports dom(document).add( element("some-name") .attr("attr1", "val1") .attr("attr2", "val2") .add(element("some-other-name")), element("more-elements") ); 

and then

 Element e3 = dom(document).findOne("some-other-name"); 

The important requirement that I have here is that I explicitly want to work with org.w3c.dom.Document , which

  • already exists
  • quite big
  • a little manipulation is required.

Thus converting org.w3c.dom.Document to JDOM, dom4j, etc. seems like a bad idea. I would prefer to wrap it with adapters.

If it does not exist, I can overturn it, as this jRTF syntax looks very nice! And for XML, this seems pretty easy to implement, since there are only a few node types. It can be as powerful as jQuery from a smooth API perspective!

+4
source share
3 answers

I found several tools that roughly did what I asked in my question:

However, at the same time, I am more inclined to fold my own. I am really a big fan of jquery, and I think jquery can be mapped to the Java API:

http://www.jooq.org/products/jOOX

+4
source

To develop your comment, Dom4J brings you closer to what you wanted:

 final Document dom = DocumentHelper.createDocument().addElement("some-name") .addAttribute("attr1", "val1") .addAttribute("attr2", "val2") .addElement("some-other-name").getDocument(); System.out.println(dom.asXML()); 

Output:

 <?xml version="1.0" encoding="UTF-8"?> <some-name attr1="val1" attr2="val2"><some-other-name/></some-name> 

I know that this is not the native DOM, but it is very similar and has very nice features for Java developers (iterators of elements, lists of live elements, etc.)

+4
source

Well, that might be silly, but why don't you implement this little API yourself? I am sure that you know the DOM API well, and it does not take you a long time to implement what you want.

Btw consider using XPath to manipulate a document (you can also implement your own mini-api over this).

+1
source

All Articles