title: A nice Java XML DOM utility
body: <p>I find myself writing the same verbose DOM manipulation code again and again:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>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 <code>org.w3c.dom</code>. The reasons for this are</p>
  
  <ol>
  <li>It's about an old and big legacy system</li>
  <li>The XML is used in many places and XSLT transformed several times to get XML, HTML, PDF output</li>
  <li>I'm just looking for convenience, not a big change.</li>
  </ol>
  
  <p>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 <a href="http://code.google.com/p/jrtf/" rel="nofollow">jRTF</a>:</p>
  
  <pre><code>// 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")
  );
  </code></pre>
  
  <p>and then</p>
  
  <pre><code>Element e3 = dom(document).findOne("some-other-name");
  </code></pre>
  
  <p>The important requirement I have here is that I explicitly want to operate on a <code>org.w3c.dom.Document</code> that</p>
  
  <ol>
  <li>already exists</li>
  <li>is pretty big</li>
  <li>needs quite a bit of manipulation</li>
  </ol>
  
  <p>So transforming the <code>org.w3c.dom.Document</code> into JDOM, dom4j, etc seems like a bad idea. Wrapping it with adapters is what I'd prefer.</p>
  
  <p>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!</p>