Problème de validation XSD
Hello,
J'ai un XML:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <Config01>
<booleanP>true</booleanP>
<booleanS>false</booleanS>
<byteS>0</byteS>
<byteP>0</byteP>
<shortS>0</shortS>
<shortP>0</shortP>
<integerS>5</integerS>
<integerP>0</integerP>
<longS>0</longS>
<floatS>0.0</floatS>
<doubleS>1.0</doubleS>
<doubleP>1.0</doubleP>
<string>aaa</string>
<tabIntP>
<int>1</int>
<int>2</int>
<int>3</int>
</tabIntP>
<tabDoubleS>
<double>1.1</double>
<double>2.2</double>
<double>3.3</double>
</tabDoubleS>
<tabString>
<string>I</string>
<string>am</string>
<string>nothing</string>
</tabString>
</Config01> |
Un XSD (généré d'après le XML avec XMLSpy 2007):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| <?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Config01">
<xs:complexType>
<xs:sequence>
<xs:element ref="booleanP"/>
<xs:element ref="booleanS"/>
<xs:element ref="byteS"/>
<xs:element ref="byteP"/>
<xs:element ref="shortS"/>
<xs:element ref="shortP"/>
<xs:element ref="integerS"/>
<xs:element ref="integerP"/>
<xs:element ref="longS"/>
<xs:element ref="floatS"/>
<xs:element ref="doubleS"/>
<xs:element ref="doubleP"/>
<xs:element ref="string"/>
<xs:element ref="tabIntP"/>
<xs:element ref="tabDoubleS"/>
<xs:element ref="tabString"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="tabString">
<xs:complexType>
<xs:sequence>
<xs:element ref="string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="tabIntP">
<xs:complexType>
<xs:sequence>
<xs:element ref="int" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="tabDoubleS">
<xs:complexType>
<xs:sequence>
<xs:element ref="double" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="string">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="I"/>
<xs:enumeration value="aaa"/>
<xs:enumeration value="am"/>
<xs:enumeration value="nothing"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="shortS">
<xs:simpleType>
<xs:restriction base="xs:byte">
<xs:enumeration value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="shortP">
<xs:simpleType>
<xs:restriction base="xs:byte">
<xs:enumeration value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="longS">
<xs:simpleType>
<xs:restriction base="xs:byte">
<xs:enumeration value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="integerS">
<xs:simpleType>
<xs:restriction base="xs:byte">
<xs:enumeration value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="integerP">
<xs:simpleType>
<xs:restriction base="xs:byte">
<xs:enumeration value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="int">
<xs:simpleType>
<xs:restriction base="xs:byte">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
<xs:enumeration value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="floatS">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:enumeration value="0.0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="doubleS">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:enumeration value="1.0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="doubleP">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:enumeration value="1.0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="double">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:enumeration value="1.1"/>
<xs:enumeration value="2.2"/>
<xs:enumeration value="3.3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="byteS">
<xs:simpleType>
<xs:restriction base="xs:byte">
<xs:enumeration value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="byteP">
<xs:simpleType>
<xs:restriction base="xs:byte">
<xs:enumeration value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="booleanS" type="xs:boolean"/>
<xs:element name="booleanP" type="xs:boolean"/>
</xs:schema> |
Et le code JAVA suivant qui est sensé me valider mon XML suivant le schéma!
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| try {
// parse an XML document into a DOM tree
DocumentBuilder parser;
parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("src/output.xml"));
// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load a WXS schema, represented by a Schema instance
Source schemaFile = new StreamSource(new File("src/config01.xsd"));
Schema schema = factory.newSchema(schemaFile);
// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();
// validate the DOM tree
validator.validate(new DOMSource(document));
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e) {
// instance document is invalid!
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} |
Mais il me met une erreur sur la ligne:
Code:
validator.validate(new DOMSource(document));
L'erreur:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Config01'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Unknown Source)
at Configuration.loadFromXML(Configuration.java:68)
at Test.main(Test.java:11)
C'est quoi qui est faux??
Merci d'avance!
A+