2012-08-09 2 views
5

J'utilise XmlPullParser pour ouvrir un fichier et XPath pour obtenir la racine. (Plus tard, je vais modifier mon code pour obtenir le nœud idx)Erreur inconnue dans xpath (en utilisant xmlpullparser)

Cependant, je reçois l'erreur suivante:

javax.xml.transform.TransformerException: Unknown error in XPath. 

J'ai cherché sur internet mais ils ont rien trouvé qui pourrait résoudre ce problème.

try{ 
    XmlPullParser xpp = getResources().getXml(R.xml.x1); 
    XPath xpath = XPathFactory.newInstance().newXPath(); 
    String askFor2 = "/root"; 
    NodeList creaturesNodes = (NodeList) xpath.evaluate(askFor2, xpp, XPathConstants.NODESET); 
    Log.d("", ""); 
} catch (Exception ex) { 
    String err = (ex.getMessage()==null)?"run thread failed":ex.getMessage(); 
    Log.e("bm run catch", err); 
} 

Mon fichier XML est

<?xml version="1.0"?> 
<root> 
    <child index="1"> 
     <idx index="1" /> 
     <idx index="2" /> 
     <idx index="3" /> 
     <idx index="4" /> 
     <idx index="5" /> 
     <idx index="6" /> 
     <idx index="7" /> 
    </child> 
</root> 

Votre temps et votre aide est très appréciée.

Répondre

0

Il me semble que vous devriez passer un InputSource comme deuxième argument à xpath.evaluate(), pas un analyseur. Gardez à l'esprit que XPath en général peut avoir besoin de parcourir une arborescence complète de documents, donc dans certains cas restreints, il ne peut pas fonctionner avec une analyse en continu: vous devez lire tout le document, construire un arbre en mémoire et appliquer XPath à cela.

2

Vous passez un mauvais attribut pour évaluer la méthode. Essayez d'utiliser InputSource,

try{ 
    XPath xpath = XPathFactory.newInstance().newXPath(); 
    InputSource is = new InputSource(getResources().openRawResource(R.raw.xm)); 
    String askFor2 = "/root"; 
    NodeList creaturesNodes = (NodeList) xpath.evaluate(askFor2, is, XPathConstants.NODESET); 
    Log.d("", ""); 
} catch (Exception ex) { 
    Log.e("bm run catch", err); 
}