commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Safdar Kureishy" <Safdar.Kurei...@bluejungle.com>
Subject Using Digester to validate an XML file against an XSD
Date Mon, 06 Dec 2004 23:31:07 GMT
Hi,

 

I couldn't find any discussion in the archives on this so am posting
this message. I'm a beginner to XML validation so forgive me if these
questions appear silly :-)

 

I've already got Digester to parse a valid XML file into my Java
objects, but am having trouble getting it to correctly validate this XML
file against an XSD schema.

 

My XSD file (Configuration.xsd) looks exactly like the following:

 

<?xml version="1.0" encoding="UTF-8"?>

 

<xs:schema targetNamespace="http://mycompany.com/a/b/c/types" 

               xmlns:xs="http://www.w3.org/2001/XMLSchema" 

               xmlns:types="http://mycompany.com/a/b/c/types">

      ...

...

            <element name="Configuration" type="types:MyComplexType"/>

      ...

            ... (schema definitions based on XSD specification)

            ...

 

 

My XML file looks exactly like the following, and is located in a
*different* folder than my XSD file:

 

<?xml version="1.0"?>

 

<RootElement xmlns="http://mycompany.com/a/b/c/types"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://mycompany.com/a/b/c/types
Configuration.xsd">

            ...

            ...

            ...

 

Now I use my digester in the following way:

            // Create a Digester object using a rules mapping file:

            Digester digester =
DigesterLoader.createDigester(rules.toURL());

 

            // Setup validation of the XML file against the XSD schema:

            digester.setSchema(schema.toString());  --> This is the
absolute path to my XSD file

            digester.setValidating(true);

            digester.setErrorHandler(new MyConfigurationErrorHandler());

            

            // Parse:

            digester.push(this);

            digester.parse(input);

 

MyConfigurationErrorHandler implements ErrorHandler and prints out the
SAXParseException objects onto the console.

 

When I execute the parse() method above, I get errors for each element
in my XML file, even though these elements are specified clearly in the
XSD file. For example, I get errors saying:

     Parse Error at line 8 column -1: Element type "Configuration" is
not declared.

     Parse Error at line 5 column -1: Element type "Name" is not
declared.

     Parse Error at line 5 column -1: Element type "Location" is not
declared.

 

Does anybody know what is the correct way of using Digester to validate
an XML file against an XSD schema? Do I need to include any additional
attributes in the XML/XSD files?

 

Thanks in advance! :-)

 

Regards,

Safdar

 

 

 


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message