Newbie: cannot get parser to validate using schema

Discussion in 'XML' started by Ghee, Mar 7, 2005.

  1. Ghee

    Ghee Guest

    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
     
    Ghee, Mar 7, 2005
    #1
    1. Advertising

  2. 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();
    }
    }
    }

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 7, 2005
    #2
    1. Advertising

  3. Ghee

    Ghee Guest

    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
     
    Ghee, Mar 7, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fhuddles
    Replies:
    1
    Views:
    972
    fhuddles
    Feb 15, 2005
  2. Markus
    Replies:
    1
    Views:
    1,580
    Markus
    Nov 23, 2005
  3. Aaron P Frenger

    Using schema to validate xml file segment

    Aaron P Frenger, Nov 4, 2004, in forum: XML
    Replies:
    0
    Views:
    513
    Aaron P Frenger
    Nov 4, 2004
  4. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,275
    Stanimir Stamenkov
    Apr 25, 2005
  5. Replies:
    2
    Views:
    944
    Joseph Kesselman
    Nov 5, 2007
Loading...

Share This Page