IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

XML/XSL et SOAP Discussion :

Problème parcours d'un fichier XML avec namespace


Sujet :

XML/XSL et SOAP

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Janvier 2008
    Messages
    57
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations forums :
    Inscription : Janvier 2008
    Messages : 57
    Points : 39
    Points
    39
    Par défaut Problème parcours d'un fichier XML avec namespace
    Bonjour ,
    je cherche depuis quelques jours à parcourir un fichier XML :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    <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 : 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 : 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
    J'ai un peu tout essayer.Quelqu'un peux m'aider SVP.
    Merci d'avance.

  2. #2
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 559
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 559
    Points : 21 621
    Points
    21 621
    Par défaut
    Deux choses :

    - je t'ai déjà dit qu'utiliser JDOM est bien plus simple que le DOM de base de Java. S'imposer d'implémenter un NamespaceContext est assez absurde, par exemple.

    - Le problème n'est pas lié à XML mais au code Java. Voyons la ligne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if("content".equals("sh")){
    Cette condition ne sera évidemment jamais vraie. la chaîne "content" n'est pas égale à la chaîne "sh". Je pense que tu as mis des guillemets en trop à "content". D'ailleurs le paramètre de la méthode ne s'appelle pas content mais prefix.

Discussions similaires

  1. Problème pour lire un fichier Xml avec Xpath
    Par adrix26 dans le forum VB.NET
    Réponses: 1
    Dernier message: 06/11/2008, 17h06
  2. [DOM4J] Problème pour "parser" un fichier XML avec accents
    Par mlny84 dans le forum Format d'échange (XML, JSON...)
    Réponses: 7
    Dernier message: 26/11/2007, 19h06
  3. [DOM4J] Problème de lecture de fichier xml avec dom4j
    Par santana2006 dans le forum Format d'échange (XML, JSON...)
    Réponses: 3
    Dernier message: 05/04/2006, 16h52
  4. problème pour parser un fichier xml avec XML::Simple
    Par black_code dans le forum Modules
    Réponses: 3
    Dernier message: 30/01/2006, 19h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo