Validate XML against Schema

Discussion in 'Java' started by Paco, Mar 6, 2006.

  1. Paco

    Paco Guest

    I'm trying to validate some XML against a schema. The code I have
    works but it won't give me any useful information for where the error
    is in the XML. I can get useful information (line numbers) if there
    are problems when loading the schema and XML. However, the exceptions
    generated from validate() always have line and column numbers of "-1".
    Am I doing something wrong or should I be going about this in a totally
    different method? My code is below (minus error handling and such).
    The SAXErrorHandler is a class I created that simply prints out the
    message and line and column numbers.

    // Load Schema
    File schemaFile = new File("schema.xsd");
    SchemaFactory factory =
    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schemaSource = new StreamSource(schemaFile);
    Schema schema = factory.newSchema(schemaSource);

    // Load XML
    File xmlFile = new File("data.xml");
    DocumentBuilder parser =
    DocumentBuilderFactory.newInstance().newDocumentBuilder();
    document = parser.parse(xmlFile);

    // Validate
    Validator validator = schema.newValidator();
    SAXErrorHandler errHandler = new SAXErrorHandler();
    validator.setErrorHandler(errHandler);
    DOMResult result = new DOMResult();
    validator.validate(new DOMSource(document), result);
     
    Paco, Mar 6, 2006
    #1
    1. Advertising

  2. Paco

    robert Guest

    Paco escreveu:

    > I'm trying to validate some XML against a schema. The code I have
    > works but it won't give me any useful information for where the error
    > is in the XML. I can get useful information (line numbers) if there
    > are problems when loading the schema and XML. However, the exceptions
    > generated from validate() always have line and column numbers of "-1".
    > Am I doing something wrong or should I be going about this in a totally
    > different method? My code is below (minus error handling and such).
    > The SAXErrorHandler is a class I created that simply prints out the
    > message and line and column numbers.
    >
    > // Load Schema
    > File schemaFile = new File("schema.xsd");
    > SchemaFactory factory =
    > SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    > Source schemaSource = new StreamSource(schemaFile);
    > Schema schema = factory.newSchema(schemaSource);
    >
    > // Load XML
    > File xmlFile = new File("data.xml");
    > DocumentBuilder parser =
    > DocumentBuilderFactory.newInstance().newDocumentBuilder();
    > document = parser.parse(xmlFile);
    >
    > // Validate
    > Validator validator = schema.newValidator();
    > SAXErrorHandler errHandler = new SAXErrorHandler();
    > validator.setErrorHandler(errHandler);
    > DOMResult result = new DOMResult();
    > validator.validate(new DOMSource(document), result);


    You could define your own Handler that extends DefaultHandler to get
    into error details - you should have more luck than -1. You don't show
    your class but implementing DefaultHandler is a good place to start.
    The point being that there are other handlers - see the sax2 o'reilly
    book or google for more info. The entityResolver shown is for locally
    resolving URI's and is probably not relevant:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);
    dbf.setNamespaceAware(true);
    dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
    DocumentBuilder db = dbf.newDocumentBuilder();
    //Add a custom error handler
    MyDefaultHandler dh = new MyDefaultHandler();
    db.setErrorHandler(dh);
    db.setEntityResolver(new SchemaLoader());
    InputSource isXml = new InputSource (new StringReader(xml));
    Document doc_in = db.parse(isXml);
    if (false == dh.isSchemaValidated)
    {
    throw new java.lang.IllegalStateException("ERR: Invalid XML
    received - schema validation failed");
    }

    //Custom error hanler to print errors and return isValid
    class MyDefaultHandler extends DefaultHandler
    {
    //flag to check if the xml document was valid
    public boolean isSchemaValidated = true;

    //Receive notification of a recoverable error.
    public void error(SAXParseException se)
    {
    setValidity(se);
    }

    //Receive notification of a non-recoverable error.
    public void fatalError(SAXParseException se)
    {
    setValidity(se);
    }

    //Receive notification of a warning.
    public void warning(SAXParseException se)
    {
    setValidity(se);
    }

    private void setValidity(SAXParseException se)
    {
    isSchemaValidated = false;
    Fwlog.error(this, Fwlog.DB, se);
    }
    }

    Now if your SAXParseException trace shows -1, than I advice with going
    with a different jar implementation in the classloader.

    HTH,
    Robert
    http://www.braziloutsource.com/
     
    robert, Mar 6, 2006
    #2
    1. Advertising

  3. Paco

    Paco Guest

    Thanks for the response Robert. My problem does stem from
    SAXParseException returning -1.

    Here is the code from my handler:

    public void error(SAXParseException e)
    {
    System.out.println("ERROR:\n"
    + " EXCEPTION: " + e.getMessage() + "\n"
    + " LINE NUM: " + e.getLineNumber() + "\n"
    + " COL NUM: " + e.getColumnNumber() + "\n");
    }


    Here is a typical exception:

    ERROR:
    EXCEPTION: cvc-complex-type.2.4.a: Invalid content was found starting
    with element 'Stuff'. One of '{"":Name}' is expected.
    LINE NUM: -1
    COL NUM: -1


    I'm working with jdk1.5.0_05. Is there another way to go about this?
    Should I use a Locator, not use a DOMSource, or what? Even if I could
    just get access to the Node that violated the schema, that would be an
    improvement.

    Thanks,
    Paco
     
    Paco, Mar 6, 2006
    #3
  4. "Paco" <> wrote in message
    news:...
    > Thanks for the response Robert. My problem does stem from
    > SAXParseException returning -1.
    >
    > Here is the code from my handler:
    >
    > public void error(SAXParseException e)
    > {
    > System.out.println("ERROR:\n"
    > + " EXCEPTION: " + e.getMessage() + "\n"
    > + " LINE NUM: " + e.getLineNumber() + "\n"
    > + " COL NUM: " + e.getColumnNumber() + "\n");
    > }
    >
    >
    > Here is a typical exception:
    >
    > ERROR:
    > EXCEPTION: cvc-complex-type.2.4.a: Invalid content was found starting
    > with element 'Stuff'. One of '{"":Name}' is expected.
    > LINE NUM: -1
    > COL NUM: -1
    >
    >
    > I'm working with jdk1.5.0_05. Is there another way to go about this?
    > Should I use a Locator, not use a DOMSource, or what? Even if I could
    > just get access to the Node that violated the schema, that would be an
    > improvement.


    I don't see how the validator could get line/column information from a
    DOMSource. DOM nodes don't contain that information. (It's possible that
    some DOM Node implementations do, but there's no uniform way to access that
    information.) It's worth trying a SAXSource instead.
     
    Mike Schilling, Mar 6, 2006
    #4
  5. Paco

    Paco Guest

    Thanks Mike. A SAXSource does give me the correct information.
    However, I need the results of the validation to be a DOM type object.
    Does anyone know how to transform a SAXResult into a DOMResult?

    Thanks,
    Paco
     
    Paco, Mar 7, 2006
    #5
    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. Replies:
    2
    Views:
    579
  2. Leona
    Replies:
    9
    Views:
    1,999
    Henry S. Thompson
    Nov 1, 2004
  3. ric_deez
    Replies:
    0
    Views:
    1,070
    ric_deez
    Apr 3, 2006
  4. Stefan
    Replies:
    3
    Views:
    1,985
    Martin Honnen
    Mar 29, 2008
  5. Shawn

    validate xml against schema

    Shawn, Jan 27, 2004, in forum: ASP General
    Replies:
    1
    Views:
    152
    Chris Hohmann
    Jan 27, 2004
Loading...

Share This Page