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 an XMLReader
and an InputSource
. In this example the dtd is located next to the annotated class and so can be found in the classpath.
Main.java
public class Main {
private static final String FEATURE_NAMESPACES = "http://xml.org/sax/features/namespaces";
private static final String FEATURE_NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
public static void main(String[] args) throws JAXBException, IOException, SAXException {
JAXBContext ctx = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
XMLReader xmlreader = XMLReaderFactory.createXMLReader();
xmlreader.setFeature(FEATURE_NAMESPACES, true);
xmlreader.setFeature(FEATURE_NAMESPACE_PREFIXES, true);
xmlreader.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
// TODO: Check if systemId really references root.dtd
return new InputSource(Root.class.getResourceAsStream("root.dtd"));
}
});
String xml = "<!DOCTYPE root SYSTEM './root.dtd'><root><element>test</element></root>";
InputSource input = new InputSource(new StringReader(xml));
Source source = new SAXSource(xmlreader, input);
Root root = (Root)unmarshaller.unmarshal(source);
System.out.println(root.getElement());
}
}
Root.java
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlElement
private String element;
public String getElement() {
return element;
}
public void setElement(String element) {
this.element = element;
}
}
root.dtd
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT root (element)>
<!ELEMENT element (#PCDATA)>
Question #2: Why does the class even need the dtd file in the first place?
It is not the JAXB implementation that is looking for the DTD, it is the underlying parser.
Question #1: Where can I locate ./dtd so that the class can find it when run as a tomcat webservice?
I'm not sure, but below I'll demonstrate a way you can make this work using the MOXy JAXB implementation (I'm the tech lead) that will work in multiple environments.
Proposed Solution
Create an EntityResolver that loads the DTD from the classpath. This way you can package the DTD with your application and you will always know where it is regardless of the deployment environment.
public class DtdEntityResolver implements EntityResolver {
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
InputStream dtd = getClass().getClassLoader().getResourceAsStream("dtd/Connections.dtd");
return new InputSource(dtd);
}
}
Then using the MOXy JAXB implementation you can cast down to the underlying implementation and set the EntityResolver.
import org.eclipse.persistence.jaxb.JAXBHelper;
...
JAXBContext jContext = JAXBContext.newInstance("com.optimumlightpath.it.aspenoss.xsd");
Unmarshaller unmarshaller = jContext.createUnmarshaller() ;
JAXBHelper.getUnmarshaller(unmarshaller).getXMLUnmarshaller().setEntityResolver(new DtdEntityResolver());
StringReader xmlStr = new StringReader(str.value);
Connections conns =(Connections) unmarshaller.unmarshal(xmlStr);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With