2010-11-15 20 views
3

Je suis en train d'obtenir une valeur d'attribut à partir d'un fichier XML, mais mon code échoue à l'exception ci-dessous:expression XPath pour obtenir une valeur d'attribut échoue en Java

11-15 16: 34: 42,270: DEBUG/XpathUtil (403): = exception javax.xml.xpath.XPathExpressionException: javax.xml.transform.TransformerException: jetons supplémentaires illégales: '@', 'source'

Voici le code que j'utilise pour obtenir la liste des nœuds:

private static final String XPATH_SOURCE = "array/[email protected]"; 
mDocument = XpathUtils.createXpathDocument(xml); 

NodeList fullNameNodeList = XpathUtils.getNodeList(mDocument, 
       XPATH_FULLNAME); 

Et voici ma XpathUtils classe:

public class XpathUtils { 

    private static XPath xpath = XPathFactory.newInstance().newXPath(); 
    private static String TAG = "XpathUtil"; 

    public static Document createXpathDocument(String xml) { 
     try { 

      Log.d(TAG , "about to create document builder factory"); 
      DocumentBuilderFactory docFactory = DocumentBuilderFactory 
        .newInstance(); 
      Log.d(TAG , "about to create document builder "); 
      DocumentBuilder builder = docFactory.newDocumentBuilder(); 

      Log.d(TAG , "about to create document with parsing the xml string which is: "); 

      Log.d(TAG ,xml); 
      Document document = builder.parse(new InputSource(
        new StringReader(xml))); 

      Log.d(TAG , "If i see this message then everythings fine "); 

      return document; 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.d(TAG , "EXCEPTION OCCURED HERE " + e.toString()); 
      return null; 
     } 
    } 

    public static NodeList getNodeList(Document doc, String expr) { 
     try { 
      Log.d(TAG , "inside getNodeList"); 
      XPathExpression pathExpr = xpath.compile(expr); 
      return (NodeList) pathExpr.evaluate(doc, XPathConstants.NODESET); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.d(TAG, "exception = " + e.toString()); 
     } 
     return null; 
    } 

    // extracts the String value for the given expression 
    public static String getNodeValue(Node n, String expr) { 
     try { 
      Log.d(TAG , "inside getNodeValue"); 
      XPathExpression pathExpr = xpath.compile(expr); 
      return (String) pathExpr.evaluate(n, XPathConstants.STRING); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

je reçois une exception levée dans la méthode getNodeList. Maintenant, selon http://www.w3schools.com/xpath/xpath_syntax.asp, pour obtenir une valeur d'attribut, vous utilisez le signe "@". Mais pour une raison quelconque, Java se plaint de ce symbole.

Répondre

6

Essayez

array/extConsumer/@source 

comme expression XPath. Ceci sélectionne l'attribut source de l'élément extConsumer.

1

Mettez une barre oblique avant la spécification d'attribut:

array/extConsumer/@source 
1

La page W3Schools vous dit aussi lié à « prédicats sont toujours intégrés entre crochets. » Vous venez d'ajouter le @source. Essayez

private static final String XPATH_SOURCE = "array/extConsumer[@source]"; 

EDIT:
Pour être clair, cela est si vous êtes à la recherche d'un seul élément, qui est ce que votre texte original m'a amené à croire. Si vous voulez rassembler un tas d'attributs sources, voyez les réponses de vanje et Anon qui suggèrent d'utiliser une barre oblique au lieu de crochets.