2012-08-09 28 views
5

Sto usando XmlPullParser per aprire un file e XPath per ottenere la radice. (Più tardi farò modificare il mio codice per ottenere il nodo idx)Errore sconosciuto in xpath (utilizzando xmlpullparser)

Tuttavia, sto ottenendo il seguente errore:

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

Ho cercato su internet, ma non ho trovato nulla che potesse risolvere il problema.

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); 
} 

Il mio file XML è

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

Il vostro tempo e aiuto è molto apprezzato.

risposta

0

Mi sembra che si debba passare un InputSource come secondo argomento a xpath.evaluate(), non un parser. Tenete presente che XPath in generale potrebbe aver bisogno di percorrere un intero albero di documenti, quindi, tranne in alcuni casi speciali limitati, non può funzionare con un'analisi di streaming: è necessario leggere l'intero documento, costruire un modello di albero in memoria e applicare XPath a quello.

2

Si sta passando all'attributo errato per valutare il metodo. Prova a utilizzare 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); 
} 
Problemi correlati