AgostinoX
AgostinoX

Reputation: 7683

java xml library based on standard dom

Since getting text content from an xml element requires 15 lines of code (see official oracle tutorial) here http://java.sun.com/webservices/reference/tutorials/jaxp/html/dom.html , the quoted tutorial itself suggests, for many needs to use thirdy party tools:

"As you can see, when you are using DOM, even a simple operation such as getting the text from a node can take a bit of programming. So if your programs handle simple data structures, then JDOM, dom4j, or even the 1.4 regular-expression package (java.util.regex) may be more appropriate for your needs"

I've tried the suggested tools and they are reasonably easy to use and complete BUT they require an evaluation of the "vitality" of their developement
. And this evaluation is not ever obvious.
So my questions are:

1) is there a library that eases xml work, built on top of standard dom? it would ensure the robusteness and up-to-dateness of official library with more usability
2) is this "usability" lack about to be adressed in some other way (perhaps some new jsr?), in the oracle plans?

Upvotes: 1

Views: 193

Answers (3)

pablosaraiva
pablosaraiva

Reputation: 2343

XStream is good and easy to use.

You can do almost everything with annotations.

http://x-stream.github.io/

Upvotes: 0

Lukas Eder
Lukas Eder

Reputation: 220752

jOOX may be precisely the library you're looking for:

  • It wraps standard Java DOM
  • It is quite lightweight
  • It is inspired by jquery, which is a proven means of simplifying DOM manipulations

An example of getting the text content of an Element:

$(document).find("element").text();
$(document).xpath("//element[3]").text();

Upvotes: 0

forty-two
forty-two

Reputation: 12817

If getting text content from XML elements is what you want, use XPath:

    String xml = "<root><p>This is some text</p><p>And this is more text</p></root>";
    XPath xpath = XPathFactory.newInstance().newXPath();
    String text = xpath.evaluate("/root/p[1]", new InputSource(
            new StringReader(xml)));
    System.out.println(text);

If you want to map Java objects to XML and back, use JAXB.

Both XPath (1.0) and JAXB are part of the JDK, or they can be replaced with later versions.

However, if you try to parse XML with regular expression, you are doomed!

Upvotes: 2

Related Questions