Best java Xml parser for managing / editing an existing XML document

TASK: I have an existing xml document (UTF-8) that uses xml namespaces and xml schema. I need to parse a specific element, add that element (which should also use xml namespace prefixes), and then write the document again.

which is the best XML parser library I should use for this TASK?

I saw the previous thread ( Best XML Parser for Java ), but was not sure if dom4j or JDOM are good for / xmlSchema namespaces and good support for UTF-8 Symbols.

Some parsers that seem to be tasks for
JDOM
dOM4J
XOM
Woodstock

Any idea which one is the best? :-) I use JDK 6 and prefer NOT to use the built-in SAX / DOM tools to complete this task because it requires me to write too much code.

It would help to have some examples of such a task.

+5
source share
4 answers

Using JDOM, taking an InputStream and creating a document:

InputStream inputStream = (InputStream)httpURLConnection.getContent();
DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
docbf.setNamespaceAware(true);
DocumentBuilder docbuilder = docbf.newDocumentBuilder();
Document document = docbuilder.parse(inputStream, baseUrl);

At this point, you have XML in the Java object. Done. Easy.

You can either use the document object or the Java API to just walk through it, or use XPath, which will be easier for me (when I recognize it).

Create an XPath object that takes a bit:

public static XPath buildXPath() {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(new AtomNamespaceContext());
    return xpath;
}


public class AtomNamespaceContext implements NamespaceContext {

    public String getNamespaceURI(String prefix) {
        if (prefix == null)
            throw new NullPointerException("Null prefix");
        else if ("a".equals(prefix))
            return "http://www.w3.org/2005/Atom";
        else if ("app".equals(prefix))
            return "http://www.w3.org/2007/app";
        else if ("os".equals(prefix))
            return "http://a9.com/-/spec/opensearch/1.1/";
        else if ("x".equals(prefix)) 
            return "http://www.w3.org/1999/xhtml";
        else if ("xml".equals(prefix))
            return XMLConstants.XML_NS_URI;
        return XMLConstants.NULL_NS_URI;
    }

    // This method isn't necessary for XPath processing.
    public String getPrefix(String uri) {
        throw new UnsupportedOperationException();
    }

    // This method isn't necessary for XPath processing either.
    public Iterator getPrefixes(String uri) {
        throw new UnsupportedOperationException();
    }
}

Then just use it, which (fortunately) does not take much time:

return Integer.parseInt(xpath.evaluate("/a:feed/os:totalResults/text()", document));
+5
source

XSLT. . . , , , xml. XML, XML DOM-.

:

<xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

, XSLT, , . , , JDK.

+5

, jOOX:

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

jOOX jQuery Java. - Java DOM. :

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

. , .

+2

It looks like you can write an xslt stylesheet to do what you want.

+1
source

All Articles