Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

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

David Delbecq

2006-09-19

Replies:

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);
    }
  }
<email xmlns="http://truc.much.com/email"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://truc.much.com/email http://truc.much.com/email.xsd">
 <test/>
</email>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://truc.much.com/email" targetNamespace="http://truc.much.com/email" elementFormDefault="qualified">

<xs:element name="email">
 <xs:complexType>
   <xs:sequence>
     <xs:element name="test" type="xs:string"/>
   </xs:sequence>
 </xs:complexType>
</xs:element>
</xs:schema>
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.