开发者

Validating XML against local DTD. Problems with EntityResolver2

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(e开发者_Go百科rrorhandler.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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜