Newbie: cannot get parser to validate using schema

G

Ghee

Dear comp.text.xml,

I'm trying (in Java) to get a DOM parser to validate a document on
parsing - but I don't seem to get what I expect. I've used XMLSPY to
verify the doc and schema. When I try to parse and validate the doc, I
get messages about trying to find a DTD(?) - "Valid documents must have
a <!DOCTYPE declaration." - but I want an xml schema, not a DTD. I
then get warnings about each and every element/attribute/etc not being
declared. It's as if it's ignoring the setAttribute call to set the
schema language. Here's the code:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
String schemaSource = "C:/risk.xsd";
String JAXP_SCHEMA_SOURCE =
"http://java.sun.com/xml/jaxp/properties/schemaSource";

factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
factory.setAttribute(JAXP_SCHEMA_SOURCE, new File(schemaSource));
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(err_handler);
document = builder.parse( new File("C:/risk.xml") );

What am I doing wrong???

Thanks for any help,

Ghee
 
M

Martin Honnen

Ghee wrote:

I'm trying (in Java) to get a DOM parser to validate a document on
parsing - but I don't seem to get what I expect. I've used XMLSPY to
verify the doc and schema. When I try to parse and validate the doc, I
get messages about trying to find a DTD(?) - "Valid documents must have
a <!DOCTYPE declaration." - but I want an xml schema, not a DTD.

Here is an example using the new schema/validation API in Java 1.5
(alias Java 5):

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import java.io.File;

import javax.xml.XMLConstants;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import org.w3c.dom.Document;

public class Test2005030701 {
public static void main (String[] args) {
try {
SchemaFactory schemaFactory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource(new
File("test2005030701Xsd.xml"));
Schema schema = schemaFactory.newSchema(schemaFile);

DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setSchema(schema);

DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();
documentBuilder.setErrorHandler(new ErrorHandler() {
public void error (SAXParseException se) {
System.out.println("Error during parsing: " + se.getMessage()
+ " at line " + se.getLineNumber());
}
public void fatalError (SAXParseException se) {
System.out.println("Fatal error during parsing: " +
se.getMessage() + " at line " + se.getLineNumber());
}
public void warning (SAXParseException se) {}
});

Document xmlDocument = documentBuilder.parse("test2005030701.xml");
System.out.println("Found " +
xmlDocument.getElementsByTagName("*").getLength() + " elements.");

}
catch (Exception e) {
e.printStackTrace();
}
}
}
 
G

Ghee

Martin,
Here is an example using the new schema/validation API in Java 1.5

I think the key to this problme is actually in the version of Java
you're using - I was using 1.4.2. I simply switched to 1.5.0, and hey
presto! Everything worked as I expected.

I'm now puzzled as to why (I've got the latest JWSDP for both versions
AFAIK) - but I'd rather get on with the project than try to find out.
Perhaps my CLASSPATH wasn't as I expected for 1.4.2 and it wasn't
picking up the later version of the JAXP jar file(s?).

I'll look at the setSchema function a little later on - it's a bit
neater than the setAttribute way of doing things.

Thanks!

Ghee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top