我有xml如下。和默認的命名空間爲http://september.examples.com/setNamespaceContext默認命名空間
addnumber,firstnumber,secondnumber
XML:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<addnumber xmlns="http://september.examples.com/">
<firstnumber>10</firstnumber>
<secondnumber>22</secondnumber>
</addnumber>
</soapenv:Body>
</soapenv:Envelope>
當我嘗試使用下面的代碼
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
xpath.setNamespaceContext(createContext(xml));
String value = xpath.compile("/soapenv:Envelope[1]/soapenv:Body[1]/addnumber[1]/firstnumber[1]").evaluate(document).toString();
我歌廳空值從Xpath的獲得價值。由於我在這裏給出的Xpath是addnumber [1]/firstnumber [1]的空名稱空間,我怎樣才能給默認命名空間的xpath?
注:的createContext()將箱子的地圖,其中將包括前綴,URI
所以我必須改變我的XPATH也是對的? –
確切地說,在XPath中使用指向默認名稱空間URI的前綴而不是僅包含前綴的元素名稱 – har07