Valider un XML avec un XSD
Bonjour,
J'amerais valider un fichier xml à partir d'un fichier xsd.
Pour cela, j'ai fais ceci :
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
|
public static void validate() {
try {
// parse an XML document into a DOM tree
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("C://java/testXML/MonXML2.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("C://java/testXML/schema.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));
s_logger.debug("Validation OK ....");
} catch (SAXException e) {
s_logger.fatal(e.getMessage(), e);
} catch (ParserConfigurationException e) {
s_logger.fatal(e.getMessage(), e);
} catch (IOException e) {
s_logger.fatal(e.getMessage(), e);
}
} |
Voici le xml :
Code:
1 2 3 4 5 6 7
| <?xml version="1.0" encoding="UTF-8"?>
<birthdate>
<month>January</month>
<day>21</day>
<year>1983</year>
</birthdate> |
Et le xsd :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="http://www.w3.org/2001/xml.xsd" />
<xs:element name="birthdate">
<xs:complexType>
<xs:sequence>
<xs:element name="month" type="xs:string" />
<xs:element name="day" type="xs:int" />
<xs:element name="year" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema> |
J'obtiens l'erreur suivante :
Citation:
2008-03-10 16:33:37,078 [main] FATAL ch.idinfo.business.baseservices.medidata.DOMValidator - cvc-elt.1: Cannot find the declaration of element 'birthdate'.
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'birthdate'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1887)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(DOMValidatorHelper.java:273)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:240)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:186)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:100)
at javax.xml.validation.Validator.validate(Validator.java:127)
at ch.idinfo.business.baseservices.medidata.DOMValidator.validate(DOMValidator.java:68)
at ch.idinfo.business.baseservices.medidata.ExportInvoiceToXML.start(ExportInvoiceToXML.java:629)
at ch.idinfo.business.baseservices.medidata.ExportInvoiceToXML.main(ExportInvoiceToXML.java:56)
Je me demande de quoi peut provenir l'erreur car birthdate est bien déclaré.... Est-ce que ça vient à cause des namespace ?
Qqun peut-il m'éclairer svp ?
Merci d'avance
possible en ligne de commande avec xmllint
Bonjour;
pour valider un fichier "xml" avec un XSchema "xsd", tu peux utiliser une classe java comme tu le fais (j'apprends a la faire moi aussi, donc je ne peux rien t'apprendre sur ça).
Mais tu peux aussi valider ton fichier en ligne de commande (par le terminal).
pour cela , tu utilises Xmllint, comme suit:
xmllint -schema ton_fichier.xsd ton_fichier.xml
j'espère que ça te sera utile.
Bon courage.:lol: