根据本地 DTD 验证 XML。 EntityResolver2 的问题
我必须根据本地 DTD 文件验证一些 XML 文件。 XML 可以没有 DOCTYPE。这是我的主要方法的主体:
XMLReader reader;
DTDManipulator dtdhandler;
XMLValidationEvendHandler errorhandler = new XMLValidationEvendHandler();
try {
dtdhandler = new DTDManipulator();
dtdhandler.setDTDentity("file:///Users/hp/Desktop/BOX/A/Ordinanze.dtd");
reader = XMLReaderFactory.createXMLReader();
reader.setErrorHandler(errorhandler);
reader.setEntityResolver(dtdhandler);
//reader.setFeature("http://xml.org/sax/features/use-entity-resolver2", true);
reader.setFeature("http://xml.org/sax/features/validation",true);
reader.parse(new InputSource("OT201011120071_045NODTD.xml"));
if(errorhandler.isNoerrors()) {
System.out.println("SUCCESS!");
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
for(int i=0; i<errorhandler.getExceptionOccurred().length; i++) System.out.println(errorhandler.getExceptionOccurred()[i].getMessage());
System.exit(0);
DTD 实现 EntityResolver2:
public class DTDManipulator implements EntityResolver2 {
private String dtd;
public void setDTDentity(String entity) {
System.out.println("DTDManipulator.setDTDentity:");
System.out.println(" - entity = "+entity);
dtd = entity;
}
@Override
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
System.out.println("DTDManipulator.resolveEntity:");
System.out.println(" - publicId = "+publicId);
System.out.println(" - systemId = "+systemId);
System.out.println(" - get back = "+"");
return new InputSource(dtd);
}
@Override
public InputSource getExternalSubset(String name, String baseURI)
throws SAXException, IOException {
System.out.println("DTDManipulator.getExternalSubset:");
System.out.println(" - name = "+name);
System.out.println(" - baseURI = "+baseURI);
System.out.println(" - get back = "+dtd);
return new InputSource(dtd);
}
@Override
public InputSource resolveEntity(String name, String publicId,
String baseURI, String systemId) throws SAXException, IOException {
System.out.println("DTDManipulator.resolveEntity:");
System.out.println(" - name = "+name);
System.out.println(" - publicId = "+publicId);
System.out.println(" - baseURI = "+baseURI);
System.out.println(" - systemId = "+systemId);
System.out.println(" - get back = "+dtd);
return new InputSource(dtd);
}
}
}
当我尝试验证 XML 而不包含 DTD 引用时,我得到了意外的行为:在文档根之前没有添加 DOCTYPE,方法 getExternalSubset
从未被调用。 我不知道为什么,我错过了什么?预先感谢,请原谅我的英语。
弗朗西斯科
I have to validate some XML files against a local DTD file. The XML could be without DOCTYPE. Here is the body of my main method:
XMLReader reader;
DTDManipulator dtdhandler;
XMLValidationEvendHandler errorhandler = new XMLValidationEvendHandler();
try {
dtdhandler = new DTDManipulator();
dtdhandler.setDTDentity("file:///Users/hp/Desktop/BOX/A/Ordinanze.dtd");
reader = XMLReaderFactory.createXMLReader();
reader.setErrorHandler(errorhandler);
reader.setEntityResolver(dtdhandler);
//reader.setFeature("http://xml.org/sax/features/use-entity-resolver2", true);
reader.setFeature("http://xml.org/sax/features/validation",true);
reader.parse(new InputSource("OT201011120071_045NODTD.xml"));
if(errorhandler.isNoerrors()) {
System.out.println("SUCCESS!");
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
for(int i=0; i<errorhandler.getExceptionOccurred().length; i++) System.out.println(errorhandler.getExceptionOccurred()[i].getMessage());
System.exit(0);
DTD implements EntityResolver2:
public class DTDManipulator implements EntityResolver2 {
private String dtd;
public void setDTDentity(String entity) {
System.out.println("DTDManipulator.setDTDentity:");
System.out.println(" - entity = "+entity);
dtd = entity;
}
@Override
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
System.out.println("DTDManipulator.resolveEntity:");
System.out.println(" - publicId = "+publicId);
System.out.println(" - systemId = "+systemId);
System.out.println(" - get back = "+"");
return new InputSource(dtd);
}
@Override
public InputSource getExternalSubset(String name, String baseURI)
throws SAXException, IOException {
System.out.println("DTDManipulator.getExternalSubset:");
System.out.println(" - name = "+name);
System.out.println(" - baseURI = "+baseURI);
System.out.println(" - get back = "+dtd);
return new InputSource(dtd);
}
@Override
public InputSource resolveEntity(String name, String publicId,
String baseURI, String systemId) throws SAXException, IOException {
System.out.println("DTDManipulator.resolveEntity:");
System.out.println(" - name = "+name);
System.out.println(" - publicId = "+publicId);
System.out.println(" - baseURI = "+baseURI);
System.out.println(" - systemId = "+systemId);
System.out.println(" - get back = "+dtd);
return new InputSource(dtd);
}
}
}
When I try to validate an XML whitout DTD references inside i got an unexpected behavior: no DOCTYPE is added before the document root, the method getExternalSubset
of DTDManipulator is never called.
I can't guess why, what am I missing? Thanks in advance, excuse my english.
Francesco
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论