Bonsoir à vous tous ,
Je veux convertir un fichier (.XML) en (.owl) je manipule le document XML en utilisant l'API JDOm et pour la création de l'ontologie j'utilise Jena , la conversion passe bien sauf au niveau des ObjectProperty le programme ne créé pas de ObjectProperty je n'ai pas pu trouver une solution efficace car j'ai trouvé des difficultés pour parcourir le document XML pour extraire le range pour se faire j'ai rajouté un attribut range pour chaque nœud que je veux rendre (ObjectProperty ) et c'est là où j'ai trouvé le problème. Les codes suivants illustre ce que je viens de dire
le document XML
voici le code 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 <?xml version="1.0" encoding="UTF-8"?> <TouristDataRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:/C:/Users/Manel/Desktop/XML/sourcedonne/doc1.xsd"> <touristData> <Tripper> <IDTripper nature="PrimaryKey" >1</IDTripper> <nameTripper>lkklkj</nameTripper> <firstNameTripper>Manel</firstNameTripper> <Adress>kkkfjj</Adress> <Email> khkhk@hotmail.com</Email> <phoneNumber>bjbjbjb </phoneNumber> </Tripper> <Motel> <ID_Motel nature="PrimaryKey">H100</ID_Motel> <ComfortLevel>*****</ComfortLevel> <nameMotel>Hilton</nameMotel> <city>London</city> <country>GB</country> </Motel> <room> <ID_room nature="PrimaryKey">R1_1</ID_room> <ID_Motel nature="ForeignKey" Range="Motel">H100</ID_Motel> </room> <Allocation> <IDTripper nature="ForeignKey" Range="Tripper">1</IDTripper> <ID_room nature="ForeignKey" Range="Room">R1_1</ID_room> <ID_Motel nature="ForeignKey" Range="Motel">H100</ID_Motel> <duration>P5M</duration> <date_start>2011-03-11</date_start> <date_end>2011-08-11</date_end> <price unite="Euro">2000</price> </Allocation> </touristData> </TouristDataRoot>
Merci d'avance
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
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 import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.List; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import com.hp.hpl.jena.ontology.DatatypeProperty; import com.hp.hpl.jena.ontology.ObjectProperty; import com.hp.hpl.jena.ontology.OntClass; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModelSpec; import com.hp.hpl.jena.rdf.model.ModelFactory; public class Main { static org.jdom2.Document document; static Element racine ; static void lireFichier(String fichier ) throws JDOMException, IOException{ SAXBuilder sxb=new SAXBuilder(); document=sxb.build(new File(fichier)); racine=document.getRootElement(); System.out.println(racine); } static void afficherElement(String element) throws FileNotFoundException{ OntModel onto ; File file=new File("DataTourist.owl"); onto =ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, null); String uriBase="http://www.ontologie.fr/DataTourist#"; onto.createOntology(uriBase); FileOutputStream fichierXML=null; List listFilsDirect =racine.getChildren(); Iterator i= listFilsDirect.iterator(); String str = ""; while(i.hasNext()){ Element courant = (Element) i.next(); while(!str.equals(courant.toString())){ str=courant.toString(); System.out.println("..........ici on affichera les tourist Data.........."); System.out.println("le fils direct est:"+courant); List listChildren=courant.getChildren(); if( listChildren!=null){ Iterator j = listChildren.iterator(); while(j.hasNext()){ Element filsCourant=(Element)j.next(); System.out.println("le fils du fils DataTourist : "+filsCourant); OntClass classe=onto.createClass(uriBase+filsCourant.getName()); //liste des datatYpe List listChildrenOfChildren =filsCourant.getChildren(); if(listChildrenOfChildren !=null){ Iterator k=listChildrenOfChildren.iterator(); while(k.hasNext()){ Element FilsFilsCourant=(Element)k.next(); DatatypeProperty prop=onto.createDatatypeProperty(uriBase+FilsFilsCourant.getName()); System.out.println(" le petit fils est : "+ FilsFilsCourant); if( FilsFilsCourant.hasAttributes()==true){ System.out.println("le sous fils du fils direct posséde un attribut "+FilsFilsCourant.getAttributes()); //if(FilsFilsCourant.getAttributeValue("nature").equals("ForeignKey") ){ if("[[Attribute: nature=ForeignKey]]".equalsIgnoreCase(FilsFilsCourant.getAttributes().toString())){ ObjectProperty prop2=onto.createObjectProperty(uriBase+FilsFilsCourant.getName()); OntClass c =onto.createClass(uriBase+FilsFilsCourant.getAttributeValue("range")) ; // System.out.println(FilsFilsCourant.getAttributeValue("Domaine").toString()); prop2.setRange(c); OntClass domaine=onto.createClass(uriBase+FilsFilsCourant.getParent()); prop2.setDomain(domaine) ; } } } } } } } } fichierXML= new FileOutputStream (file); onto.write (fichierXML); } public static void main(String[] args) throws JDOMException, IOException { // TODO Auto-generated method stub lireFichier("touristData.xml"); afficherElement("racine"); } }
Cordialement
Partager