Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Newbie: cannot get parser to validate using schema

Reply
Thread Tools

Newbie: cannot get parser to validate using schema

 
 
Ghee
Guest
Posts: n/a
 
      03-07-2005
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-07-2005


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_SCH EMA_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();
}
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Ghee
Guest
Posts: n/a
 
      03-07-2005
Martin,

Martin wrote:
>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
build a hierarchical tree, without using DOM,schema, and sax using expat parser and c pharioum@gmail.com XML 2 11-05-2007 02:24 PM
Using Python to validate XML against an XSD schema ric_deez Python 0 04-03-2006 09:27 PM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Unable to validate XML Schema with SAX Parser: fhuddles Java 1 02-15-2005 02:58 PM
Using schema to validate xml file segment Aaron P Frenger XML 0 11-04-2004 05:16 PM



Advertisments