I have a class that unmarshals xml from a 3rd party source (I have no control over the content). Here is the snippet that unmarshals:
JAXBContext jContext = JAXBContext.newInstance("com.optimumlightpath.it.aspenoss.xsd");
Unmarshaller unmarshaller = jContext.createUnmarshaller() ;
StringReader xmlStr = new StringReader(str.value);
Connections conns = (Connections) unmarshaller.unmarshal(xmlStr);
Connections is a class generated dtd->xsd->class using xjc. The package com.optimumlightpath.it.aspenoss.xsd contains all such classes.
The xml I recieve contains a relative path in the DOCTYPE. Basically str.value above contains:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE Connections SYSTEM "./dtd/Connections.dtd">
<Connections>
...
</Connections>
This runs successfully as a java 1.5 application. In order to avoid the error above, I had to create a ./dtd directory off the project root and include all the dtd files (not sure why I had to do this but we’ll get to that).
I’ve since created a web service on Tomcat5.5 that uses the above class. I am getting [org.xml.sax.SAXParseException: Relative URI "./dtd/Connections.dtd"; can not be resolved without a document URI.] on the unmarshal line. I have tried creating ./dtd in every relavant folder (project root, WebContent, WEB-INF, tomcat working dir, etc) to no avail.
Question #1: Where can I locate ./dtd so that the class can find it when run as a tomcat webservice? Is there any tomcat or service config I need to do in order to get the directory recognized?
Question #2: Why does the class even need the dtd file in the first place? Doesn’t it have all the information it needs to unmarshal in the annotations of the dtd->xsd->class? I’ve read many posts about disabling validation, setting EntityResource, and other solutions, but this class isn’t always deployed as a web-service and I don’t want to have two code trains.
When unmarshalling from an InputStream or Reader the parser does not know the systemId (uri / location) of the document, so it can not resolve relative paths. It seems the parser tries to resolve references using the current working directory, which only works when running from the ide or command line. In order to override this behaviour and do the resolving yourself you need to implement an
EntityResolver, as Blaise Doughan mentioned.After some experimenting I found a standard way of doing this. You need to unmarshal from a
SAXSource, which is in turn constructed from anXMLReaderand anInputSource. In this example the dtd is located next to the annotated class and so can be found in the classpath.Main.java
Root.java
root.dtd