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

Discussion in 'XML' started by Xavier Seneque, Jul 9, 2005.

  1. 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);
    Xavier Seneque, Jul 9, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Jessel
    Replies:
    0
    Views:
    394
    Ben Jessel
    Aug 5, 2004
  2. Markus
    Replies:
    1
    Views:
    1,499
    Markus
    Nov 23, 2005
  3. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,173
    Stanimir Stamenkov
    Apr 25, 2005
  4. First_step_to_xml
    Replies:
    0
    Views:
    695
    First_step_to_xml
    Jun 28, 2006
  5. Replies:
    3
    Views:
    2,800
Loading...

Share This Page