0
我想使用下面的代碼得到XPATH的結果。無論我使用的實際XPATH表達式如何,我總是得不到任何結果。Android中的XPath沒有結果
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
builder = factory.newDocumentBuilder();
doc = builder.parse(getResources().openRawResource(R.raw.cdatest02));
NodeList nodes;
String exp = "//versionNumber";
XPathFactory xfactory = XPathFactory.newInstance();
XPath xpath = xfactory.newXPath();
xpath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String s) {
if (s.equals(XMLConstants.DEFAULT_NS_PREFIX))
return doc.lookupNamespaceURI(null);
else
return doc.lookupNamespaceURI(s);
}
public String getPrefix(String s) {
return doc.lookupPrefix(s);
}
public Iterator getPrefixes(String s) {
return null;
}
});
nodes = (NodeList) xpath.evaluate(exp, doc,XPathConstants.NODESET);
Log.i("xpath results", "nothing returned");
我懷疑這個問題與XML的命名空間有關。 XML具有聲明的默認名稱空間。
XML開始像這樣(非常截斷)。
<?xml version="1.0" encoding="UTF-8"?>
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:npfitlc="NPFIT:HL7:Localisation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" classCode="DOCCLIN" moodCode="EVN">
<typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>
<npfitlc:messageType root="2.16.840.1.113883.2.1.3.2.4.18.17" extension="POCD_MT000012GB01"/>/
<id root="A709A442-3CF4-476E-8377-376500E829C9"/>
<code code="1234567" codeSystem="2.16.840.1.113883.2.1.3.2.4.15"/>
<title mediaType="text/plain" representation="TXT">Patient Health Record Summary</title>
<effectiveTime value="201305192000+01"/>
<confidentialityCode code="V" displayName="very restricted" codeSystem="2.16.840.1.113883.5.25"/>
<setId root="411910CF-1A76-4330-98FE-C345DDEE5553"/>
<versionNumber value="1"/>
.... lots more lines of XML ...,
有關我哪裏出錯的想法嗎?
奇怪 - 幾分鐘前有一個正確的答案,現在它已經走了...... –