AgostinoX AgostinoX - 9 months ago 52
Java Question

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 , 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?


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)));

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!