根据本地 DTD 验证 XML。 EntityResolver2 的问题

发布于 2024-12-05 14:18:56 字数 2858 浏览 0 评论 0原文

我必须根据本地 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文