I was asked to contribute to a side project at work. This small java project should handle XML file from an external repository, modify and reinject them in an ETL.
XML and Java ? A tumultuous love story...
Since I am not a big fan of JAXP and SAX, I searched for another XML manipulation library and found JDOM.
JDOM is a powerful Java library which aim to ease XML manipulation. It was once included in JSR 102 however the JSR is now withdrawn (more information can be found in the JDOM mailing list).
Since it is not the main purpose, if you want to learn more about the library there is a great primer writen in github wiki's page : https://github.com/hunterhacker/jdom/wiki/JDOM2-A-Primer
One of the downside of current JDOM version 2.0.2 is maven integration. When you want to include JDOM in your project, you add the following dependency :
Now if you try to perform mvn install, you will face one of the following errors :
In summary, here is the depency set I currently use for JDOM 2.0.2 :
XML and Java ? A tumultuous love story...
Since I am not a big fan of JAXP and SAX, I searched for another XML manipulation library and found JDOM.
JDOM is a powerful Java library which aim to ease XML manipulation. It was once included in JSR 102 however the JSR is now withdrawn (more information can be found in the JDOM mailing list).
Since it is not the main purpose, if you want to learn more about the library there is a great primer writen in github wiki's page : https://github.com/hunterhacker/jdom/wiki/JDOM2-A-Primer
One of the downside of current JDOM version 2.0.2 is maven integration. When you want to include JDOM in your project, you add the following dependency :
<dependency> <groupId>org.jdom</groupId> <artifactId>jdom</artifactId> <version>2.0.2</version> </dependency>And JDOM pom.xml references those dependencies :
<dependencies> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.3</version> <optional>true</optional> </dependency> <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.10.0</version> <optional>true</optional> </dependency> <dependency> <groupId>xalan</groupId> <artifactId>xalan</artifactId> <version>2.7.1</version> <optional>true</optional> </dependency> </dependencies>
Now if you try to perform mvn install, you will face one of the following errors :
- The following artifacts could not be resolved: maven-plugins:maven-cobertura-plugin:plugin:1.3, maven-plugins:maven-findbugs-plugin:plugin:1.3.1, Failure to find maven-plugins:maven-cobertura-plugin:plugin:1.3 in ...
<dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.3</version> <optional>true</optional> <!-- http://jira.codehaus.org/browse/JAXEN-217 --> <exclusions> <exclusion> <groupId>maven-plugins</groupId> <artifactId>maven-cobertura-plugin</artifactId> </exclusion> <exclusion> <groupId>maven-plugins</groupId> <artifactId>maven-findbugs-plugin</artifactId> </exclusion> </exclusions> </dependency>
java.lang.ExceptionInInitializerError at org.jdom2.input.SAXBuilder.
(SAXBuilder.java:338) at org.jdom2.input.SAXBuilder. (SAXBuilder.java:221) at [...] Caused by: java.lang.UnsupportedOperationException: This parser does not support specification "null" version "null" at javax.xml.parsers.SAXParserFactory.setSchema(SAXParserFactory.java:421) at org.jdom2.input.sax.XMLReaders. (XMLReaders.java:122) at org.jdom2.input.sax.XMLReaders. (XMLReaders.java:95) ... 30 more
This should not work if your JDK is lesser than 1.5.<dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.3</version> <optional>true</optional> <!-- Use JDK xerces version --> <exclusions> <exclusion> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> </exclusion> </exclusions> </dependency>
In summary, here is the depency set I currently use for JDOM 2.0.2 :
<dependency> <groupId>org.jdom</groupId> <artifactId>jdom</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.3</version> <exclusions> <!-- Use JDK xerces version --> <exclusion> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> </exclusion> <!-- http://jira.codehaus.org/browse/JAXEN-217 --> <exclusion> <groupId>maven-plugins</groupId> <artifactId>maven-cobertura-plugin</artifactId> </exclusion> <exclusion> <groupId>maven-plugins</groupId> <artifactId>maven-findbugs-plugin</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>xalan</groupId> <artifactId>xalan</artifactId> <version>2.7.1</version> </dependency>
Had you take a look at any XPP3 implementation like xstream or mxp1?
ReplyDeleteFrom what I have read, XStream could not be used on the project since XML are generated by partners and structures are changing/not known.
ReplyDeleteIs there a way to modify XML without defining structure inside classes ?
concerning MXP1, the documentation is not really inspiring...
Thanks for the post, I followed your direction but I still get this error:
ReplyDeleteException in thread "main" java.lang.ExceptionInInitializerError
at org.jdom2.input.SAXBuilder.(SAXBuilder.java:338)
..
Caused by: java.lang.UnsupportedOperationException: This parser does not support specification "null" version "null"
at javax.xml.parsers.SAXParserFactory.setSchema(SAXParserFactory.java:421)
at org.jdom2.input.sax.XMLReaders.(XMLReaders.java:122)
at org.jdom2.input.sax.XMLReaders.(XMLReaders.java:95)
... 6 more
And my only line of code is :
SAXBuilder builder = new SAXBuilder();
Needless to say, it works when I provide jdom.jar explicitly, it only happens when I use maven.
I found a temporary solution! I used jdom 1.1 and there was no problem:
ReplyDeletejdom
jdom
1.1
The only thing I had to change was to explicitly cast anElement.getChildren() as (List)anElement.getChildren()
Thank you very much... but the same thing happens that dumb-genius.... I'm using JDOM2 2.0.3.
ReplyDeleteI switched my project from JDom 2.0.2 to 2.0.3 without issue.
ReplyDeleteI suspect that your problem is not linked to JDom version but to a conflict with xml-apis.jar or xmlParserAPIs.jar.
Can you post a small project with a failing test ?
And can you post your project's mvn depency:tree ?
ReplyDelete