Bonjour,
J'utilise JAXB pour recuperer le contenu du fichier xml, mais avant de le recuperer je lui donne un schema dont le contenu est le suivant :
ROOT.xsd :
Mon fichier xml à la forme suivante (j'ai supprimé les autres balises du niveau bas) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:DGE="urn:ops:rncps:dge" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="urn:ops:rncps:dge" schemaLocation="ROOT_DOCUMENT.xsd"/> <!-- MAIN INSTANCES GLOBAL ELEMENTS --> <xsd:element name="Documents" type="Documents"/> <!-- PARTIAL BRANCH <<AGREGATE>> --> <!-- CURRENT BRANCH TOP TYPE --> <xsd:complexType name="Documents"> <xsd:sequence> <xsd:group ref="DGE:Document" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <!-- END OF SCHEMA --> </xsd:schema>
Pour lire le fichier xml, j'utilise ces instructions de JAXB :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <?xml version="1.0" encoding="UTF-8"?> <Documents xmlns="urn:ops:rncps:dge" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ops:rncps:dge ROOT.xsd"> <Document> <Mindex> <Profil>CP1</Profil> <Identification>CI1</Identification> <Temps>2012-06-27T11:39:15+02:00</Temps> <RefDGE>CodeRef1</RefDGE> </Mindex> </Document> </Documents>
J'obtiens cette erreur qui me semble que l'element 'Documents' n'est pas déclarée dans le fichier xsd, alors qu'il exite :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schema = schemaFactory.newSchema(new File("src/main/java/dev/xsd/", "ROOT.xsd")); File xmlDocuments = new File("src/main/resources/xml/docs3.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Documents.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); jaxbUnmarshaller.setSchema(schema); Documents docs = (Documents) jaxbUnmarshaller.unmarshal(xmlDocuments);
Si je met pas le schema, la lecture du fichier xml se passe bien sans aucune erreur.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Documents'.] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:503) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:204) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169) at faycal.dev.JAXBExemple.main(JAXBExemple.java:88)
Partager