[jdom-interest] How to validate xml using xsd?

Avlonitis,Nick nick.avlonitis at transcore.com
Tue Sep 19 14:22:05 PDT 2006


I'm having quite the similar problem.  I can use an XSD schema to
validate XML directly without any issue, yet when trying to apply the
same validation to the same XML represented as a JDom Document, I get
the following exception:

org.jdom.JDOMException: Exception in startElement: cvc-elt.1: Cannot
find the declaration of element 'Integer'.

Attached are the java source and input XML and XSD files used for
processing.  You'll need the org.jdom libraries and XStream object
serialization libraries
(http://dist.codehaus.org/xstream/jars/xstream-1.2.jar) included in your
classpath in order to compile & run successfully.

Drop all 3 in the same directory to run.  The first try/catch block is
XML validation directly with XSD which completes successfully.  2nd
try/catch is XML conversion to JDOM, then validation of JDOM with same
XSD, which fails.

-----Original Message-----
From: jdom-interest-bounces at jdom.org
[mailto:jdom-interest-bounces at jdom.org] On Behalf Of David Delbecq
Sent: Tuesday, September 19, 2006 5:05 AM
To: jdom-interest at jdom.org
Subject: [jdom-interest] How to validate xml using xsd?

Hi

i am using the code below to validate an xml using an xsd. The
corresponding xml and xsd have been attached. Despit the xml being valid
according to my editor, which validates against the xsd, i keep getting
this error with jdom:
org.jdom.input.JDOMParseException: Error on line 3: cvc-elt.1: Cannot
find the declaration of element 'email'.

As far as i know, my xsd is valid as well as the xml. Is there something
more i need to do in my code to have validation successfull?

code:
public void testXmlFormat() {
       
        SAXBuilder builder = new SAXBuilder(true);
        builder.setEntityResolver(new EntityResolver() {
       
            public InputSource resolveEntity(String publicId, String
systemId)
                    throws SAXException, IOException {
                //code to return the resource corresponding to
http://truc.much.com/email.xsd and printing on stderr the full url to
resource
                //return null for other systemId;
            }
       
        });
        try {
            builder.setProperty( JAXPConstants.JAXP_SCHEMA_LANGUAGE,
                    JAXPConstants.W3C_XML_SCHEMA );
            InputStream source =
getClass().getClassLoader().getResourceAsStream("email.xml");
            InputSource is = new InputSource(source);
            builder.build(is);
        } catch (JDOMException e) {
            e.printStackTrace();
            fail("Exception thrown: "+e);
        } catch (IOException e) {
            e.printStackTrace();
            fail("Exception thrown: "+e);
        }
    }
Disclaimer: 
This email may contain confidential and privileged material for the sole use of the intended recipient(s) and only for those purposes previously or herein authorized by the sender. Any review, use, distribution or disclosure by others, or use by the recipient for unauthorized purposes, is strictly prohibited. If you are not the intended recipient (or authorized to receive for the recipient), please contact the sender by reply email and delete all copies of this message.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mySchema2.xsd
Type: application/octet-stream
Size: 374 bytes
Desc: mySchema2.xsd
Url : http://www.jdom.org/pipermail/jdom-interest/attachments/20060919/70461fcf/mySchema2.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testXml2.xml
Type: text/xml
Size: 89 bytes
Desc: testXml2.xml
Url : http://www.jdom.org/pipermail/jdom-interest/attachments/20060919/70461fcf/testXml2.xml
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SchemaParser.java
Type: application/octet-stream
Size: 3422 bytes
Desc: SchemaParser.java
Url : http://www.jdom.org/pipermail/jdom-interest/attachments/20060919/70461fcf/SchemaParser.obj


More information about the jdom-interest mailing list