Reputation: 3467
I have followed Obtaining DOCTYPE details using SAX (JDK 7), implementing it like this:
public class MyXmlReader {
public static void parse(InputSource inputSource) {
try {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
MyContentHandler handler = new MyContentHandler();
xmlReader.setContentHandler(handler);
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); // Does not work; handler is set, but startDTD/endDTD is not called
xmlReader.setDTDHandler(handler);
xmlReader.setErrorHandler(new MyErrorHandler());
xmlReader.setFeature("http://xml.org/sax/features/validation", false);
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
xmlReader.parse(inputSource);
}
catch (SAXException e) {
throw new MyImportException("Error while parsing file", e);
}
}
}
MyContentHandler extends DefaultHandler2, but neither startDTD nor endDTD is called (but e.g. startEntity is in fact called, so the lexical handler is set). I have tried to leave the features out, but this changes nothing.
What goes wrong here? I am using Java 8 JDK 1.8.0_144.
The XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE MyMessage SYSTEM "http://www.testsite.org/mymessage/5.1/reference/international.dtd">
<MyMessage>
<Header>
...
Upvotes: 3
Views: 415
Reputation: 1394
According to XMLReader API you need to set a DTD Handler, otherwise DTD Events will be silently ignored. A DefaultHandler2
yet implements DTDHandler interface, so you could use xmlReader.setDTDHandler(handler);
again;
Upvotes: 1