I am trying to validate my XML with local DTD, I did google about this then I got some code, here is my code.
public Document buildDocument(File receivedFile) {
Document doc = null;
try {
logger.info("Inside buildDocument() , create a new DocumentBuilderFactory");
// create a new DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
// use the factory to create a documentbuilder
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new ErrorHandler() {
@Override
public void fatalError(SAXParseException exception) throws SAXException {
System.err.println("fatalError: " + exception);
}
@Override
public void error(SAXParseException exception) throws SAXException {
System.err.println("error: " + exception);
}
@Override
public void warning(SAXParseException exception) throws SAXException {
System.err.println("warning: " + exception);
}
});
builder.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
if (systemId.contains("xyz.com/remote.dtd")) {
return new InputSource(FileUtils.readFileToString(
new File("C:\\Users\\xyz\\local.dtd"));
} else {
return null;
}
}
});
doc = builder.parse(new InputSource(new StringReader(FileUtils.readFileToString(receivedFile, "UTF-16"))));
} catch (ParserConfigurationException | SAXException | IOException e) {
logger.warn("Opps got error while buiding document", e);
}
return doc;
I am getting following error, I am sure the local DTD works fine with the XML which I am trying to validate. I don't find any issue with DTD but still I get this error please help on this.
> java.net.MalformedURLException: no protocol: <!DOCTYPE ichicsr [
> <!ENTITY lt "&#60;">
>
> <!-- Greater Than ">" --> <!ENTITY gt ">">
>
> <!-- Ampersand "&" --> <!ENTITY amp "&#38;">
Prints the entire DTD then!
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:964)
at org.apache.xerces.impl.XMLEntityManager.startEntity(XMLEntityManager.java:902)
at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(XMLEntityManager.java:869)
at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(XMLDTDScannerImpl.java:241)
at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(XMLDocumentScannerImpl.java:1001)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:324)
at org.apache.xerces.parsers.XML11Configuration.parse(XML11Configuration.java:875)
at org.apache.xerces.parsers.XML11Configuration.parse(XML11Configuration.java:798)
at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:108)
at org.apache.xerces.parsers.DOMParser.parse(DOMParser.java:230)
NOTE : My XML is encoded type UTF-16
Update : Removed UF-16 while reading the file, looks like DTD is trying to get compile and it's throwing me following error ,
latin-entities.dtd (The system cannot find the path specified)
Does it mean is this DTD is looking for dependent DTD ??