Skip to main content

Validation issues in JAXP

1 reply [Last post]
Joined: 2008-02-04

I apologize for cross posting/repetition, if any.
I'm still using JDK 1.5 and have recently initiated an effort to move to JAXP, from Apache Xerces.

DocumentBuilder parser = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

if (validate) {
My earlier parsers seamlessly were able to handle DTDs and Schema validations without any hassle. However, if I call

I am able to handle Schema based validations only. If I comment that line out, and just have
factory.setValidating(true) ,
I am able to handle DTDs alone.

Is there anyway to be able to dynamically switch from DTDs to XSD and vice -versa? I have a range of XML documents that either use one of these or does not want any validation at all.

Message was edited by: srinimurthy

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-12-15

You may want to try JAXP 1.4 which supports DOM Level 3. JAXP 1.4 is in jdk6, but you can also get the latest jaxp 1.4 and use it in jdk5 with the endorsed mechanism.

I tried using your code with JAXP 1.4 to parse a couple of files, one with a dtd and another schema. I was able to parse both documents using the factory created in your code:
DocumentBuilder builder = factory.newDocumentBuilder();
Document document1 = builder.parse( xmlFile );
Document document2 = builder.parse( xmlFile1 );