Here is how you can capture the doctype information if you use JAXB with StAX.
XMLResolver (MyXMLResolver)
Here is an implementation of XMLResolver
you can use to capture the doctype information.
import javax.xml.stream.XMLResolver;
import javax.xml.stream.XMLStreamException;
public class MyXMLResolver implements XMLResolver {
private String publicID;
private String systemID;
private String baseURI;
private String namespace;
@Override
public Object resolveEntity(String publicID, String systemID,
String baseURI, String namespace) throws XMLStreamException {
this.publicID = publicID;
this.systemID = systemID;
this.baseURI = baseURI;
this.namespace = namespace;
return null;
}
public String getPublicID() {
return publicID;
}
public String getSystemID() {
return systemID;
}
public String getBaseURI() {
return baseURI;
}
public String getNamespace() {
return namespace;
}
}
Demo
Here is how you leverage it in your use case:
import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
MyXMLResolver resolver = new MyXMLResolver();
xif.setXMLResolver(resolver);
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("input.xml"));
JAXBContext jc = JAXBContext.newInstance(Power.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Power power = (Power) unmarshaller.unmarshal(xsr);
System.out.println(resolver.getPublicID());
System.out.println(resolver.getSystemID());
System.out.println(resolver.getBaseURI());
System.out.println(resolver.getNamespace());
}
}