私はXPathの初心者で、次の問題があります。
Web サービスからデータを受け取る Java メソッドがあり、これらのデータは XML ドキュメント内にあるため、XPath を使用してこの XML 結果ドキュメント内の特定の値を取得する必要があります。
特に、これは Web サービス (Web サービスの応答) によって提供される XML 出力全体です。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<getConfigSettingsResponse xmlns="http://tempuri.org/">
<getConfigSettingsResult><![CDATA[<root>
<status>
<id>0</id>
<message></message>
</status>
<drivers>
<drive id="tokenId 11">
<shared-secret>Shared 11</shared-secret>
<encoding>false</encoding>
<compression />
</drive>
<drive id="tokenId 2 ">
<shared-secret>Shared 2 </shared-secret>
<encoding>false</encoding>
<compression>false</compression>
</drive>
</drivers>
</root>]]></getConfigSettingsResult>
</getConfigSettingsResponse>
</s:Body>
</s:Envelope>
Java クラスで、次の操作を実行します。
XPath xPath; // An utility class for performing XPath calls on JDOM nodes
Element objectElement; // An XML element
//xPath = XPath.newInstance("s:Envelope/s:Body/getVersionResponse/getVersionResult");
try {
// XPath selection:
xPath = XPath.newInstance("s:Envelope/s:Body");
xPath.addNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
objectElement = (Element) xPath.selectSingleNode(documentXML);
if (objectElement != null) {
result = objectElement.getValue();
System.out.println("RESULT:");
System.out.println(result);
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
結果変数の内容を出力した結果は、次の出力です。
RESULT:
<root>
<status>
<id>0</id>
<message></message>
</status>
<drivers>
<drive id="tokenId 11">
<shared-secret>Shared 11</shared-secret>
<encoding>false</encoding>
<compression />
</drive>
<drive id="tokenId 2 ">
<shared-secret>Shared 2 </shared-secret>
<encoding>false</encoding>
<compression>false</compression>
</drive>
</drivers>
</root>
ここで私の問題は、0タグのコンテンツにのみアクセスしたいということです。そのため、(この場合)結果変数に0値を含める必要があります。
しかし、できません。以前の XPath の選択を次のように変更しようとしました。
xPath = XPath.newInstance("s:Envelope/s:Body/s:status/s:id");
しかし、このようにして、objectElementがnullであることを取得します
なんで?私は何が欠けていますか?mu 結果変数にidタグの内容が含まれるようにするにはどうすればよいですか?
TNX
アンドレア