Précédent   Forum du club des développeurs et IT Pro > Autres langages > XML/XSL et SOAP > APIs
APIs DOM, SAX, JAXP,STAX... leur fonctionnement, leurs implémentations Avant de poster -> FAQ XML
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 19/05/2010, 14h29   #1
ramroum1986
Invité régulier
 
Homme
Inscription : janvier 2008
Messages : 55
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Tunisie

Informations forums :
Inscription : janvier 2008
Messages : 55
Points : 7
Points : 7
Par défaut Problème avec namespace pour parcours xml

Bonjour ,
je cherche depuis quelques jours à parcourir un fichier XML :
Code :
Code :
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>
je cherche à obtenir valeur
voici mon code java
Code :

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
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 :

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
 
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
J'ai un peu tout essayer.Quelqu'un peux m'aider SVP.
Merci d'avance.
ramroum1986 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 20/05/2010, 11h24   #2
ramroum1986
Invité régulier
 
Homme
Inscription : janvier 2008
Messages : 55
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Tunisie

Informations forums :
Inscription : janvier 2008
Messages : 55
Points : 7
Points : 7
Bonjour,
je voulais ajouter que getNamespaceURI(prefix) me retourne une valeur nulle. je sais pas quel est la cause.
Alors si vous avez une idée ??
Merci.
ramroum1986 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 20/05/2010, 11h33   #3
Morbo
Membre Expert
 
Inscription : septembre 2006
Messages : 1 466
Détails du profil
Informations forums :
Inscription : septembre 2006
Messages : 1 466
Points : 1 597
Points : 1 597
En effet cette méthode te retournera toujours une valeur null car tu ne testes jamais la valeur du paramètre d'entrée .
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
			}
		}
Morbo est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 20/05/2010, 12h46   #4
ramroum1986
Invité régulier
 
Homme
Inscription : janvier 2008
Messages : 55
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Tunisie

Informations forums :
Inscription : janvier 2008
Messages : 55
Points : 7
Points : 7
Ok je réalise bien maintenant ce que j'ai fait :
Merci .
ramroum1986 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 10h15.


 
 
 
 
Partenaires

Hébergement Web