analysing an XML document and validating with a schema ...

X

Xavier Seneque

hello,

i'm trying to parse an XML file from a java program, and the structure
of the XML document is described in a schema file.

here is the simple schema file (departement.xsd) :
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="departement">
<xs:complexType>
<xs:sequence>
<xs:element name="ville" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="nom" type="xs:string"/>
<xs:element name="codepostal" type="xs:positiveInteger"/>
<xs:element name="latitude" type="xs:decimal"/>
<xs:element name="longitude" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="num" type="xs:positiveInteger" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>

my XML file looks somewhat like that (villes-31.xml):
<?xml version="1.0" encoding="UTF-8"?>
<departement num="31"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="departement.xsd">
<ville>
<nom>Agassac</nom>
<codepostal>31230</codepostal>
<latitude>43.366667</latitude>
<longitude>0.883333</longitude>
</ville>
...
</departement>

and when i parse the xml file with my java program, i get those errors :
Error: URI = "file:villes-31.xml", Line = "2", : Document root element
"departement", must match DOCTYPE root "null".
Error: URI = "file:villes-31.xml", Line = "2", : Document is invalid: no
grammar found.
Error: URI = "file:villes-31.xml", Line = "2", : cvc-complex-type.3.2.2:
Attribute 'xsi:schemaLocation' is not allowed to appear in element
'departement'.
Error: URI = "file:villes-31.xml", Line = "2", : cvc-complex-type.3.2.2:
Attribute 'xsi:schemaLocation' is not allowed to appear in element
'departement'.

but i just did exactly the same as in w3schools.com ... so i don't
understand why he doesn't like my line 2 !

anyone could tell me why ?

thank you in advance !
xs

ps : here is what my java prog looks like :

fabrique = DocumentBuilderFactory.newInstance();
sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schema = sf.newSchema(fichierSchema);
fabrique.setSchema(schema);
fabrique.setValidating(true);
fabrique.setIgnoringElementContentWhitespace(true);
constructeur = fabrique.newDocumentBuilder();
document = constructeur.parse(fichierXML);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top