Bonjour à tous,
Je suis revenu vers vous car j'ai un souci avec mon application java développé pour valider un fichier XML avec un fichier XSD donnée.
J'utilisé JDom.
Le problème :
Quand je lance l'application par exemple( sans la balise <nom>.....</nom>)
l'application envoie True alors que la balise nom manque, de même pour les balises <personne>,... ça serait sympa & de votre part de me proposer une solution pour ce problem. Je vous remercie à l'avance
fichier Person.xml
fichier Person.xsd
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 <?xml version="1.0" encoding="UTF-8"?> <personnes xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="Person.xsd"> <personne> <!--<nom>dupon</nom>--> <prenom>pierre</prenom> </personne> <personne> <nom>durant</nom> <prenom>jean</prenom> </personne> <personne> <nom>martin</nom> <prenom>gilles</prenom> </personne> </personnes>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0"> <xsd:element name="personnes"> <xsd:complexType> <xsd:sequence> <xsd:element name="personne" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="nom" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="prenom" type="xsd:string" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
application java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import java.io.File; import javax.xml.*; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.jdom.*; import org.jdom.input.SAXBuilder; public class Validate{ private SAXBuilder sxb; private Document document; private SchemaFactory factory; private Source schemaFile; private Schema schema; private Validator validator; private boolean ok; public Validate() throws Exception{ this.sxb = new SAXBuilder(); } public boolean XMLFile() throws Exception { this.ok =false; try { this.document =sxb.build(new File("C://Project_Valid_xml_by_xsd//resources//Person.xml")); this.factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); this.schemaFile = new StreamSource(new File("C://Project_Valid_xml_by_xsd//resources//Person.xsd")); this.schema = factory.newSchema(schemaFile); this.validator = schema.newValidator(); this.validator.validate(new DOMSource()); this.ok = true; String s = String.valueOf(ok); System.out.println(s); } catch (Exception e) { e.printStackTrace(); System.out.println("PB : "+e.getMessage());; } return this.ok; } public static void main(String[] args) throws Exception { Validate v = new Validate(); v.XMLFile(); } }
Partager