简体   繁体   中英

java ignore the DTD for validation if a schema is available

Is this possible to ignore an internal DTD for validation, when a document has XSD schema available?

DTD should be used for validation if no schema specified in the source document.

Here's how I configure the parser.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
factory.setFeature("http://apache.org/xml/features/validation/dynamic", true);

factory.setSchema(schemaFactory.newSchema());
builder.setErrorHandler(errorHandler);

I use the "dynamic" feature to make the parser perform DTD validation only if the DTD is specified.

I cannot get rid of DTD validation at all, I need to ignore it only if the source XML document has XSD schema.

Sometimes the DTD declaration is used only to declare DTD entities and the schema reference is used for validation against a W3C XML Schema.

The problem is DTD validation can be performed only at the time of parsing and before that I don't know whether a document has internal references to DTD or XSD.

It's certainly possible to prevent the DTD being used for validation, but it's not so easy to prevent it being used for entity expansion (and therefore being read). If you want to prevent it being read entirely, and if you know this won't cause a problem with entity expansion, you can supply the parser with an EntityResolver that replaces the DTD with a dummy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM