1

I'm trying to find a Java library that stays on top of DOM/XPath and provides an object-oriented interface to XML manipulations. Would be nice to have something like this, for example:

// all X... classes are from the wrapping library
XDocument xdoc = new XDocument(new File("abc.xml"));
xdoc.find("//abc/foo").find("bar").text("one two three");

Something similar to jQuery, but for Java domain.

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • 4
    Uhm ... how is DOM not object oriented? – Joachim Sauer Mar 08 '11 at 15:35
  • NodeList is not iterable, for example. The biggest problem is that DOM was invented before XPath, and they are not integrated as tightly as in jQuery, for example. – yegor256 Mar 08 '11 at 15:45
  • @yegor: that has nothing to do with object-orientation. DOM is not a nice API to work with and it's often lacking integration with the "host environment" (a.k.a a `NodeList` should be a (i.e. implement) `List` in Java), that much is true. But those problems do not make it any less OO. – Joachim Sauer Mar 08 '11 at 15:48
  • @Joachim agree. I'm looking for a library which is "nice to work with" and which stays on top of DOM, meaning that it doesn't re-implement XML manipulations, but instead make calls to DOM. – yegor256 Mar 08 '11 at 15:51
  • @yegor256 why over DOM if the library provides complete control and access to the XML? – mmmmmm Mar 15 '11 at 12:33
  • @Mark because DOM implementation is interchangeable, but a custom library is still a custom library. If its authors abandon the project I will get stuck with its defects and problems – yegor256 Mar 15 '11 at 12:47
  • @yegor256 but won't there be the same issue with the custom library covering DOM? – mmmmmm Mar 15 '11 at 13:03
  • @Mark yes, you're right, but the complexity of this library will be much lower, as well as the likelihood of fatal bugs. – yegor256 Mar 15 '11 at 13:18
  • Jdom is built on a XML parser (I suspect using SAX) and can read DOM objects - so is very similar to what you want + has not needed to be fixed for 18 months – mmmmmm Mar 15 '11 at 13:59
  • It's been a while since I used it, but I happily used Dom4J in the past – Romski Jul 12 '12 at 14:05

6 Answers6

1

Only one that comes to mind is JDOM ( http://www.jdom.org ).

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
cjstehno
  • 13,468
  • 4
  • 44
  • 56
  • JDOM site says: "While JDOM interoperates well with existing standards such as the Simple API for XML (SAX) and the Document Object Model (DOM), it is not an abstraction layer or enhancement to those APIs". It means that JDOM is not **on top** of DOM. Right? – yegor256 Mar 08 '11 at 15:27
  • Correct but the query is XPath - why do you the DOM exactly – mmmmmm Mar 08 '11 at 15:33
1

If Groovy still qualifies as 'for Java domain', you could also checkout Groovy's XMLSlurper, which can be used to parse/update XML.

revdrjrr
  • 1,025
  • 1
  • 8
  • 16
1

Would something like JAXB work for you?

Instead of working with the concept of an XML, you parse the tree into a set of Java object that you can work with. If you familiar at all with JPA then JAXB will seem very natural.

The Java objects can even be created automatically from an XSD file using XJC.

Mark Robinson
  • 3,135
  • 1
  • 22
  • 37
  • While JAXB does not wrap the DOM, you can use the JAXB Binder to maintain a link between the objects and DOM and apply changes between the model: http://bdoughan.blogspot.com/2010/09/jaxb-xml-infoset-preservation.html – bdoughan Mar 08 '11 at 16:44
0

I created my own: jcabi-xml. It wraps DOM and XPath, for example:

XML xml = new XMLDocument(
  "<orders><order id="4">Coffee to go</order></orders>"
);
String id = xml.xpath("//order/@id").get(0);
String name = xml.xpath("//order[@id=4]/text()");
System.out.println(xml.toString());
yegor256
  • 102,010
  • 123
  • 446
  • 597
0

XMLBeans is the best Java/XML object framework out there:

http://xmlbeans.apache.org/

but you need an XSD for the XML you're parsing. You then do stuff like:

abcDocument.Factory.parse(new File("abc.xml");
abcType[] abcs = abcDocument.getAbc().getAbcArray();
for (abcType abc : abcs) {
  abc.getFoo().getBar();
}
codebrane
  • 4,290
  • 2
  • 18
  • 27
  • It doesn't really matter whether it's DOM or SAX (it's streaming SAX on demand - i.e. only when you access an object as a Java class). The point is you don't need DOM or SAX. You just work at the Java Object API level based on the xsd. – codebrane Mar 13 '11 at 16:15
  • XMLBeans was developed before JAXB became a standard. If you want to operate on XML on a Java Object level, then I'd prefer JAXB! – Lukas Eder Aug 12 '11 at 08:30
0

I have just now asked the same question:

A nice Java XML DOM utility

And I have found these solutions that are similar:

But xmltool is not as powerful as jQuery's fluent API, gwtquery and jsoup are built for HTML documents with CSS, etc, jsoup doesn't wrap org.w3c.dom but created its own non-standard DOM model

So I'm rolling my own:

http://code.google.com/p/joox/

Some examples:

// Find the order at index for and add an element "paid"
$(document).find("orders").children().eq(4).append("<paid>true</paid>");

// Find those orders that are paid and flag them as "settled"
$(document).find("orders").children()
           .find("paid").after("<settled>true</settled>");

// Add a complex element
$(document).find("orders").append(
  $("order", $("date", "2011-08-14"),
             $("amount", "155"),
             $("paid", "false"),
             $("settled", "false")).attr("id", "13");
Community
  • 1
  • 1
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509