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

APIs XML Discussion :

Problème avec namespace pour parcours xml


Sujet :

APIs XML

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    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
    Par défaut Problème avec namespace pour parcours xml
    Bonjour ,
    je cherche depuis quelques jours à parcourir un fichier XML :
    Code :
    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>
    je cherche à obtenir valeur
    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 :

    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
    Membre confirmé
    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
    Par défaut
    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.

  3. #3
    Membre Expert
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    1 466
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 1 466
    Par défaut
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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;
    			}
    		}

  4. #4
    Membre confirmé
    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
    Par défaut
    Ok je réalise bien maintenant ce que j'ai fait :
    Merci .

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [DOM] Problème avec DOM pour XML
    Par g0g059 dans le forum Format d'échange (XML, JSON...)
    Réponses: 2
    Dernier message: 01/07/2010, 11h18
  2. [DOM] Problème avec DOM pour le XML...
    Par Julien_riquelme dans le forum Format d'échange (XML, JSON...)
    Réponses: 4
    Dernier message: 02/03/2006, 14h51
  3. problème avec strtok pour récupérer les vides
    Par manikou dans le forum MFC
    Réponses: 4
    Dernier message: 02/06/2005, 20h08
  4. Réponses: 5
    Dernier message: 27/08/2003, 11h45

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