====== XML API ====== ===== XML processing ===== We all know that standard Java DOM implementation by Sun, which is bundled with JDK since early eras, has certain disadvantages: * ''org.w3c.dom.NodeList'' does not provide a common Java collections interface iterate through it. * There are no traversal algorithms support, the only two methods are ''org.w3c.dom.Document#getElementById()'' and ''org.w3c.dom.Document#getElementsByTagName()''. These limitations do not come out from bad model design but are the result of strict following [[http://www.w3.org/DOM/DOMTR|W3C specifications]]. The way out is to use alternative libraries, which compensate these limitations plus provide some extra bonuses. ^ Framework ^ [[http://xerces.apache.org/xerces2-j/|Xerces]] / W3C DOM ^ [[http://erikandcolleen.com/erik/projects/exml/docs/guide/|Electic XML]] ([[http://mirrors.ibiblio.org/pub/mirrors/maven2/exml/exml/|jar]]) ^ [[http://ws.apache.org/commons/axiom/|AXIOM]]((The description in this column about AXIOM model is incomplete)) ([[http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/ws/commons/axiom/|jar]]) ^ [[http://dom4j.sourceforge.net/|dom4j]] v2.x ^ [[http://dom4j.sourceforge.net/|dom4j]] v1.6.1 ([[http://mirrors.ibiblio.org/pub/mirrors/maven2/dom4j/dom4j/|jar]]) ^ [[http://www.jdom.org/|jDOM]] ([[http://mirrors.ibiblio.org/pub/mirrors/maven2/jdom/jdom/|jar]]) ^ [[http://www.xom.nu/|XOM]] ([[http://mirrors.ibiblio.org/pub/mirrors/maven2/xom/xom/|jar]]) ^ | Implements ''org.w3c.dom'' interfaces? | :YES: | :YES: | :YES: | :NO: | :NO: | :NO: | :NO: | | Are Java5 collections enabled? | :NO: | :NO: | :NO: | :YES: | :NO: | :NO: | :NO: | | Provides interfaces for all model elements? | :YES: | :YES:((Only W3C DOM interfaces)) | :YES: | :YES: | :YES: | :NO: | :NO: | | Are model elements ''java.io.Serializable''? | :NO: | :YES: | :NO: | :YES: | :YES: | :YES: | :NO: | | Are model elements ''java.lang.Cloneable''? | :NO:((Supports node cloning via ''[[javase>docs/api/org/w3c/dom/Node.html#cloneNode(boolean)|Node#cloneNode()]]'')) | :YES: | :YES: | :YES: | :YES: | :YES: | :NO: | | [[wp>Fluent interface#Java|Fluent API]]? | :NO: | :YES: | :NO: | :YES: | :YES: | :YES: | :NO: | | Quick serialization to XML | :NO: | [[http://erikandcolleen.com/erik/projects/exml/docs/glue/api/electric/xml/Child.html#toString()|:YES:]] | [[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/impl/dom/ElementImpl.html#toString()|:YES:]] | [[http://dom4j.sourceforge.net/apidocs/org/dom4j/Node.html#asXML()|:YES:]] | [[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Node.html#asXML()|:YES:]] | :NO: | [[http://www.xom.nu/apidocs/nu/xom/Node.html#toXML()|:YES:]] | | Quick ''Element.getFirstChildElement(String name)'' | :NO: | [[http://erikandcolleen.com/erik/projects/exml/docs/glue/api/electric/xml/Parent.html#getElement(java.lang.String)|:YES:]] | [[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/OMContainer.html#getFirstChildWithName(javax.xml.namespace.QName)|:YES:]] | [[http://dom4j.sourceforge.net/apidocs/org/dom4j/Element.html#element(java.lang.String)|:YES:]] | [[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Element.html#element(org.dom4j.QName)|:YES:]] | :NO: | [[http://www.xom.nu/apidocs/nu/xom/Element.html#getFirstChildElement(java.lang.String)|:YES:]] | | Quick ''Element.setAttribute(String name, String value)'' | ''[[javase>docs/api/org/w3c/dom/Element.html#setAttribute(java.lang.String,%20java.lang.String)|Element#setAttribute(String, String)]]'' | [[http://erikandcolleen.com/erik/projects/exml/docs/glue/api/electric/xml/Element.html#setAttribute(java.lang.String,%20java.lang.String)|:YES:]] | [[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/impl/dom/ElementImpl.html#setAttribute(java.lang.String,%20java.lang.String)|:YES:]] | [[http://dom4j.sourceforge.net/apidocs/org/dom4j/Element.html#addAttribute(java.lang.String,%20java.lang.String)|:YES:]] | [[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Element.html#addAttribute(java.lang.String,%20java.lang.String)|:YES:]] | [[http://www.jdom.org/docs/apidocs/org/jdom/Element.html#setAttribute(java.lang.String,%20java.lang.String)|:YES:]] | [[http://www.jdom.org/docs/apidocs/org/jdom/Element.html#getChild(java.lang.String)|:YES:]] | | Quick ''Element.setText(String)'' | ''[[javase>docs/api/org/w3c/dom/Node.html#setTextContent(java.lang.String)|Node#setTextContent(String)]]'' | [[http://erikandcolleen.com/erik/projects/exml/docs/glue/api/electric/xml/Element.html#setText(java.lang.String)|:YES:]] | [[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/impl/dom/ElementImpl.html#setText(java.lang.String)|:YES:]] | [[http://dom4j.sourceforge.net/apidocs/org/dom4j/Node.html#setText(java.lang.String)|:YES:]] | [[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Node.html#setText(java.lang.String)|:YES:]] | [[http://www.jdom.org/docs/apidocs/org/jdom/Element.html#setText(java.lang.String)|:YES:]] | [[http://www.jdom.org/docs/apidocs/org/jdom/Element.html#appendChild(java.lang.String)|:YES:]] | | Memory-efficient processing of big XML files | :NO: | :NO: | :YES: | [[http://dom4j.sourceforge.net/apidocs/org/dom4j/io/SAXReader.html#addHandler(java.lang.String,%20org.dom4j.ElementHandler)|:YES:]]((Consumer's ''ElementHandler'' should detach the node from the tree after processing)) | [[http://dom4j.sourceforge.net/dom4j-1.6.1/faq.html#large-doc|:YES:]] | :NO: | [[http://www.xom.nu/apidocs/nu/xom/NodeFactory.html#finishMakingElement(nu.xom.Element)|:YES:]]((Custom ''NodeFactory'' implementation should return empty node list)) | | Visitor pattern support (DOM tree traversal) | :NO: | :NO: | :NO:((Traversing can be done by using ''[[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/impl/OMNavigator.html|OMNavigator]]'')) | [[http://dom4j.sourceforge.net/apidocs/org/dom4j/Node.html#accept(org.dom4j.Visitor)|:YES:]] | [[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Node.html#accept(org.dom4j.Visitor)|:YES:]] | :NO: | :NO: | | Building from/to DOM | :DEL: | :DEL: | :NO: / :NO: | ''[[http://dom4j.sourceforge.net/apidocs/org/dom4j/io/DOMReader.html#read(org.w3c.dom.Document)|DOMReader#read(Document)]]'' / ''[[http://dom4j.sourceforge.net/apidocs/org/dom4j/io/DOMWriter.html#write(org.dom4j.Document)|DOMWriter#write(Document)]]'' | ''[[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/DOMReader.html#read(org.w3c.dom.Document)|DOMReader#read(Document)]]'' / ''[[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/DOMWriter.html#write(org.dom4j.Document)|DOMWriter#write(Document)]]'' | ''[[http://www.jdom.org/docs/apidocs/org/jdom/input/DOMBuilder.html#build(org.w3c.dom.Document)|DOMBuilder#build(Document)]]'' / ''[[http://www.jdom.org/docs/apidocs/org/jdom/output/DOMOutputter.html#output(org.jdom.Document)|DOMOutputter#output(Document)]]'' | ''[[http://www.xom.nu/apidocs/nu/xom/converters/DOMConverter.html#convert(org.w3c.dom.Document)|DOMConverter#convert(Document)]]''/:NO: | | Building from/to SAX | :NO: | :NO: | ''[[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/impl/builder/StAXOMBuilder.html|StAXOMBuilder#getDocument()]]'' / ''[[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/impl/serialize/OMXMLReader.html|OMXMLReader]]'' | ''[[http://dom4j.sourceforge.net/apidocs/org/dom4j/io/SAXContentHandler.html#createDocument()|SAXContentHandler#createDocument()]]'' / :NO: | ''[[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/SAXContentHandler.html#createDocument()|SAXContentHandler#createDocument()]]'' / :NO: | ''[[http://www.jdom.org/docs/apidocs/org/jdom/input/SAXHandler.html#getDocument()|SAXHandler#getDocument()]]'' / ''[[http://www.jdom.org/docs/apidocs/org/jdom/output/SAXOutputter.html#output(org.jdom.Document)|SAXOutputter#output(Document)]]'' | :NO:((XOM has a hanlder ''nu.xom.XOMHandler'', but it is non-public class)) / ''[[http://www.xom.nu/apidocs/nu/xom/converters/SAXConverter.html#convert(nu.xom.Document)|SAXConverter#convert(Document)]]'' | | Building from/to StAX | ''XMLDOMWriterImpl'' via ''[[javaee>api/javax/xml/stream/XMLOutputFactory.html#createXMLStreamWriter(javax.xml.transform.Result)|XMLOutputFactory#createXMLStreamWriter(Result)]]'' / :NO: | :YES: / :NO:((As Electic XML implements W3C DOM interfaces, any existing adapters can be used including ones bundled in JDK)) | ''[[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/impl/builder/StAXBuilder.html#StAXBuilder(javax.xml.stream.XMLStreamReader)|StAXBuilder#StAXBuilder(XMLStreamReader)]]'' / ''[[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/OMSerializable.html#serialize(javax.xml.stream.XMLStreamWriter)|OMSerializable#serialize(XMLStreamWriter)]]'' | :NO: / ''[[http://dom4j.sourceforge.net/apidocs/org/dom4j/io/STAXEventWriter.html#STAXEventWriter(javax.xml.stream.util.XMLEventConsumer)|STAXEventWriter(XMLEventConsumer)]]'' | :NO: / ''[[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/STAXEventWriter.html#STAXEventWriter(javax.xml.stream.util.XMLEventConsumer)|STAXEventWriter(XMLEventConsumer)]]'' | :NO: / :NO: | :NO: / :NO: | | XSTL transformation (TrAX) | ''[[javase>docs/api/javax/xml/transform/dom/DOMSource.html|DOMSource]]'', ''[[javase>docs/api/javax/xml/transform/dom/DOMResult.html|DOMResult]]'' | :YES:((As Electic XML implements W3C DOM interfaces, any existing adapters can be used including ones bundled in JDK)) | ''[[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/impl/jaxp/OMSource.html|OMSource]]'', ''[[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/impl/jaxp/OMResult.html|OMResult]]'' | ''[[http://dom4j.sourceforge.net/apidocs/org/dom4j/io/DocumentSource.html|DocumentSource]]'', ''[[http://dom4j.sourceforge.net/apidocs/org/dom4j/io/DocumentResult.html|DocumentResult]]'' | ''[[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/DocumentSource.html|DocumentSource]]'', ''[[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/DocumentResult.html|DocumentResult]]'' | ''[[http://www.jdom.org/docs/apidocs/org/jdom/transform/JDOMSource.html|JDOMSource]]'', ''[[http://www.jdom.org/docs/apidocs/org/jdom/transform/JDOMResult.html|JDOMResult]]'', ''[[http://www.jdom.org/docs/apidocs/org/jdom/transform/XSLTransformer.html#transform(org.jdom.Document)|XSLTransformer.html#transform(Document)]]'' | ''[[http://www.xom.nu/apidocs/nu/xom/xslt/XSLTransform.html#transform(nu.xom.Document)|XSLTransform#transform(Document)]]''((''nu.xom.xslt.XOMSource'' and ''nu.xom.xslt.XOMResult'' are non-public classes)) | | XPath search | :NO: | [[http://erikandcolleen.com/erik/projects/exml/docs/glue/api/electric/xml/jaxen/JaxenXPath.html|:YES:]]((Implementation is build on the top of [[http://jaxen.org/|Jaxen]] project)) | [[http://ws.apache.org/commons/axiom/apidocs/org/apache/axiom/om/xpath/AXIOMXPath.html#AXIOMXPath(java.lang.String)|:YES:]]((Implementation is build on the top of [[http://jaxen.org/|Jaxen]] project)) | [[http://dom4j.sourceforge.net/apidocs/org/dom4j/Node.html#selectNodes(java.lang.String)|:YES:]]((Implementation is build on the top of [[http://jaxen.org/|Jaxen]] project)) | [[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Node.html#selectNodes(java.lang.String)|:YES:]]((Implementation is build on the top of [[http://jaxen.org/|Jaxen]] project)) | [[http://www.jdom.org/docs/apidocs/org/jdom/xpath/XPath.html#selectNodes(java.lang.Object)|:YES:]]((Implementation is build on the top of [[http://jaxen.org/|Jaxen]] project)) | [[http://www.xom.nu/apidocs/nu/xom/Node.html#query(java.lang.String)|:YES:]]((Implementation is build on the top of [[http://jaxen.org/|Jaxen]] project)) | | XPath for given node | :NO: | :NO: | :NO: | [[http://dom4j.sourceforge.net/apidocs/org/dom4j/Node.html#getPath()|:YES:]] | [[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Node.html#getPath()|:YES:]] | :NO: | :NO: | | XML Schema Data Type support((Framework support for creating a schema datatype checking model elements)) | :NO: | :NO: | :NO: | [[http://dom4j.sourceforge.net/apidocs/org/dom4j/datatype/DatatypeDocumentFactory.html|:YES:]] | [[http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/datatype/DatatypeDocumentFactory.html|:YES:]] | :NO: | :NO: | | [[wp>XInclude]] support | :NO: | :NO: | :NO: | :NO: | :NO: | :NO: | [[http://www.xom.nu/apidocs/nu/xom/xinclude/XIncluder.html#resolve(nu.xom.Document)|:YES:]] | | [[wp>Canonical XML]] support | :NO: | :NO: | :NO: | :NO: | :NO: | :NO: | [[http://www.xom.nu/apidocs/nu/xom/canonical/Canonicalizer.html#write(nu.xom.Node)|:YES:]] | | License | [[wp>GPL]] | [[http://erikandcolleen.com/erik/projects/exml/docs/license/EXML-license.html|EXML license]] (OpenSource, copyright) | [[wp>Apache license]] | [[wp>BSD licence]] | [[wp>BSD licence]] | [[wp>Apache license]] | [[wp>LGPL]] | \\ Personal notes: * [[googlecode>p/xmltool/wiki/Manual|XMLTool]]: \\ :ADD: Provides a **true** [[wp>Fluent interface#Java|fluent API]] on the top of W3C DOM JDK implementation. It provides you the mechanism to create, navigate and navigate DOM tree in a stateful manner. * [[http://commons.apache.org/digester/|Digester]]: \\ :ADD: A nice lightweight solution to you define a XML-to-Java object mapping, which is based on XML pattern rules which are triggered when the given XML path is recognized. \\ :ADD: Designed to target divorced big XML files. * Electic XML: \\ :DEL: The serialization and parsing of XML tree is included into tree model elements. This triggers the following limitations: * One can read/write only to ''String'', ''File'', ''OutputStream'' or ''Writer'' at the moment. * One can probably write an adapter for [[wp>XML#Pull_parsing|pull parsers]] (e.g. ''[[javaee>api/javax/xml/stream/XMLEventReader.html|XMLEventReader]]''), but not for [[wp>XML#Simple_API_for_XML_.28SAX.29|push parsers]] (e.g. ''[[javase>docs/api/org/xml/sax/ContentHandler.html|ContentHandler]]''). * So there is no way to provide another version of parser that for example creates the XML tree with elements that extend the basic (e.g. I want to replace the standard ''Attribute'' implementation with mine, which extends the standard) * AXOIM: \\ :ADD: This is natural AXIOM facility not to create the complete XML tree model as the pulling the elements from builder occurs when you request the needed information from the model. * jDOM: \\ :DEL: If choosing between jDOM and XOM -- take XOM as it is jDOM's successor. \\ :DEL: jDOM has no natural DOM elements class hierarchy (e.g. ''Document'', ''Attribute'', ''Element'' is a ''Node'') \\ :DEL: Have a look at [[http://lists.xml.org/archives/xml-dev/200410/msg00492.html|XOM vs. dom4j]] notes by Elliotte Rusty Harold (author of XOM and also the contributor of JDOM). * XOM: \\ :DEL: The community is so limited, that nobody cares about releasing new versions of XOM into maven. \\ :DEL: XOM aims to [[http://www.artima.com/intv/jdom2.html|enforce correctness]] better than JDOM/dom4j, but the API variety is lower, also because: * XOM has a lot of final classes and methods. This prevents the programmers to mis-us the public API, but also reduces the code re-use and functionality improvements by extending the provided classes. * XOM developers are very resistant to improving API, see my threads [[http://lists.ibiblio.org/pipermail/xom-interest/2010-September/004145.html|StaX/DOM/SAX-to-XOM bridge]], [[http://lists.ibiblio.org/pipermail/xom-interest/2010-September/004147.html|XSLTransform class API]], [[http://lists.ibiblio.org/pipermail/xom-interest/2010-September/004158.html|Serialization API]], [[http://lists.ibiblio.org/pipermail/xom-interest/2010-March/004076.html|Visitor pattern]] Relative links: * [[http://dom4j.sourceforge.net/dom4j-1.6.1/compare.html|dom4j comparison to other frameworks]] * [[http://sosnoski.com/opensrc/xmlbench/results.html|Benchmark of XPP2, EXML, Xerces 1/2 DOM, Xerces 1/2 DOM with deferred node expansion, dom4j, JDOM, Crimson DOM, Xerces 1/2 SAX2, Crimson SAX]] * [[stackoverflowa>6998870/267197|simple comparison between DOM (Xecrces in JDK 1.6), DOM4J 1.6.1 and JDOM 1.1]]: see [[https://docs.google.com/viewer?pid=explorer&chrome=true&srcid=0ByTv6dYxia-xOGE0ZTQ0NDQtYWVlNi00YTlhLWI0ZTMtNzM1ZDFhMDZkNjJk|archive]] * [[stackoverflow>831865|What Java XML library do you recommend?]] * [[http://www.cafeconleche.org/books/xmljava/chapters/ch05s08.html|ElectricXML chapter]] by Elliotte Rusty Harold. * [[http://articles.techrepublic.com.com/5100-10878_11-1046617.html|Electric XML toolkit: Free and easy to use]] ===== XML serialization ===== * [[http://xstream.codehaus.org/|XStream]] * [[http://jibx.sourceforge.net/|JiBX]] * [[https://jaxb.dev.java.net/|JAXB]] * [[http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/xml.html|Hibernate]] (see [[software:hibernate]]) * [[http://castor.org/reference/html-single/index.html|Castor]] * [[stackoverflow>413597|What is the best Java OXM library?]] {{tag>XML DOM SAX Hibernate}}