Bonjour ,
je cherche depuis quelques jours à parcourir un fichier XML :
Code :
je cherche à obtenir valeur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 <sh:titi xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" xmlns:eanucc="urn:ean.ucc:2" xmlns:gdsn="urn:ean.ucc:gdsn:2"> <eanucc:tata> <gdsn:toto> <valeur> 2456</valeur> </gdsn:toto> </eanucc:tata> </sh:titi>
voici mon code java
Code :
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 public class parser{ static NamespaceContext namespace = new NamespaceContext(){ public String getNamespaceURI(String prefix){ if("content".equals("sh")){ return "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"; } else if ("content".equals("gdsn")){ return "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"; } else if ("content".equals("eanucc")){ return "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"; } else{ return null; } } public String getPrefix(String namespaceURI){ if("http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader".equals(namespaceURI)){ return "sh"; } else if("http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader".equals(namespaceURI)){ return "gdsn"; } else{ return null; } } public Iterator getPrefixes(String namespaceURI){ return null; } }; public static String evaluerSAX(File file, String expression, QName retour) throws FileNotFoundException, XPathExpressionException{ //création de la source InputSource source = new InputSource(new FileInputStream(file)); //création du XPath XPathFactory fabrique = XPathFactory.newInstance(); XPath xpath = fabrique.newXPath(); xpath.setNamespaceContext(namespace); XPathExpression exp = xpath.compile(expression); Object resultat = exp.evaluate(source,retour); System.out.println(resultat); String value = resultat.toString(); return (value); } public static void main(String[] args){ try{ File xml = new File("templates/exemple.xml"); namespace.getNamespaceURI("sh"); namespace.getNamespaceURI("eanucc"); namespace.getNamespaceURI("gdsn"); String tot = evaluerSAX(xml,"//sh:titi/seanucc:tata/gdsn:toto/valeur",XPathConstants.STRING); System.out.println(tot); }catch(Exception e){ e.printStackTrace(); } } }
Mais il y a un problème de définition des namespace. Le compilateur me rend cette erreur:
Code :
J'ai un peu tout essayer.Quelqu'un peux m'aider SVP.
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 com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Le préfixe doit se convertir en espace de noms : sh at com.sun.org.apache.xpath.internal.compiler.XPathParser.errorForDOM3(XPathParser.java:653) at com.sun.org.apache.xpath.internal.compiler.Lexer.mapNSTokens(Lexer.java:638) at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:265) at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:96) at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:110) at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176) at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:394) at ssessai.evaluerSAX(ssessai.java:78) at ssessai.main(ssessai.java:103) --------------- linked to ------------------ javax.xml.xpath.XPathExpressionException at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:402) at ssessai.evaluerSAX(ssessai.java:78) at ssessai.main(ssessai.java:103) Caused by: com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Le préfixe doit se convertir en espace de noms : sh at com.sun.org.apache.xpath.internal.compiler.XPathParser.errorForDOM3(XPathParser.java:653) at com.sun.org.apache.xpath.internal.compiler.Lexer.mapNSTokens(Lexer.java:638) at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:265) at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:96) at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:110) at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176) at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:394) ... 2 more
Merci d'avance.
Partager