[jdom-interest] JDOM SAXBuilder validation schema with Java

Steve Condas scondas at yahoo.com
Mon Dec 27 07:58:57 PST 2004


Chris,

The schema locations referenced in your XML are, by
specification, only hints to the parser.  You can
explicitly set the locations for a given namespace,
and the nonamespace, via the Xerces properties.  These
properties are described at 

http://xml.apache.org/xerces2-j/properties.html

Given your XML, I believe that you want to be setting
the
http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation
property to the local location of your no namespace
schema.

Steve
--- Christian Avril
<christian.avril at dpma.finances.gouv.fr> wrote:

> JDOM SAXBuilder validation schema
>
-----------------------------------------------------
> Does somebody know if we are obliged to mention the
> name of the schema xsd just inside of the file XML ?
> OR if we can place it only in the program Java ?
> 
> --------------------
> For example in Java,
> --------------------
> when I use only the builder.setFeature() ...
> it'works very well, because the name and the path of
> the schema xsd 
> is already written in the XML file, and I don't need
> to use the builder.setProperty()  : 
> ---------------------------------------------------
> ...
> 
>     SAXBuilder builder =  new
> SAXBuilder("org.apache.xerces.parsers.SAXParser",
> true);
>    
>
builder.setFeature("http://apache.org/xml/features/validation/schema",
> true);
>     //
>
builder.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
>     //                   
> "file:///C:/Projets/Jdom-1-0/xml/countries.xsd");
> 
> ...    
> --------------------------
> File countries.xml
> --------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <countries
>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> 
> xsi:noNamespaceSchemaLocation="countries.xsd">    
> <!-- the name of my schema is in the file XML ! -->
>  <country name="England" capital="London"/>
>  <country name="Wales" capital="Cardiff"/>
>  <country name="Scotland" capital="Edinburgh"/>
> </countries>
> 
> --------------------------
> File countries.xsd
> --------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema
> xmlns:xs="http://www.w3.org/2001/XMLSchema">
>  <xs:element name="countries">
>   <xs:complexType>
>    <xs:sequence minOccurs="0" maxOccurs="unbounded">
>     <xs:element ref="country"/>
>    </xs:sequence>
>   </xs:complexType>
>  </xs:element>
>  <xs:element name="country">
>   <xs:complexType>
>    <xs:attribute name="name" type="xs:string"
> use="required"/>
>    <xs:attribute name="capital" type="xs:string"
> use="required"/>
>   </xs:complexType>
>  </xs:element>
> </xs:schema>
> 
> 
> 
> But, if I don't want to have the name of the schema
> reference into the XML file, but only in the java
> program, 
> how Can I do ? I think I need the
> builder.setProperty() 
> But if I delete the name of the schema from the XML
> file, I have the error : 
> ------------------
> Error on line 3 of document
> file:///C:/Projets/Jdom-1-0/xml/countries.xml: 
> cvc-elt.1: Cannot find the declaration of element
> 'countries'.
> 
> And I know there is no problem with the path
> C:/Projets/Jdom-1-0/xml/countries.xml
> 
> Can somebody help ?
> 
> Thank's in advance
> I hope this problem can help somebody
> 
> Chris>
_______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com


More information about the jdom-interest mailing list