Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A nice Java XML DOM utility

Tags:

java

dom

xml

I find myself writing the same verbose 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 don't want to switch architecture all together, i.e. I don't want to use JDOM, JAXB, or anything else. Just Java's org.w3c.dom. The reasons for this are

  1. It's about an old and big legacy system
  2. The XML is used in many places and XSLT transformed several times to get XML, HTML, PDF output
  3. I'm just looking for convenience, not a big change.

I'm just wondering if there is a nice wrapper library (e.g. with apache commons or google) that allows me to do things like this with a fluent style similar to 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 I have here is that I explicitly want to operate on a org.w3c.dom.Document that

  1. already exists
  2. is pretty big
  3. needs quite a bit of manipulation

So transforming the org.w3c.dom.Document into JDOM, dom4j, etc seems like a bad idea. Wrapping it with adapters is what I'd prefer.

If it doesn't exist, I might roll my own, as this jRTF syntax looks really nice! And for XML, it seems quite easy to implement, as there are only few node types. This could become as powerful as jquery from the fluent API perspective!

like image 274
Lukas Eder Avatar asked Jan 20 '23 00:01

Lukas Eder


2 Answers

To elaborate my comment, Dom4J gets you pretty close 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 it's not native DOM, but it's very similar and it has very nice features for Java developers (element iterators, live element lists etc.)

like image 154
Sean Patrick Floyd Avatar answered Jan 30 '23 21:01

Sean Patrick Floyd


I found some tools that roughly do what I asked for in my question:

  • http://code.google.com/p/xmltool/
  • http://jsoup.org/

However, in the mean time, I am more inclinded to roll my own. I'm really a big fan of jquery, and I think jquery can be mapped to a Java fluent API:

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

like image 28
Lukas Eder Avatar answered Jan 30 '23 20:01

Lukas Eder