1

私は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");

しかし、このようにして、objectElementnullであることを取得します

なんで?私は何が欠けていますか?mu 結果変数にidタグの内容が含まれるようにするにはどうすればよいですか?

TNX

アンドレア

4

2 に答える 2

3

「CDATA」セクションの「ルート」ノード。セクション全体がテキストとして解釈され、xPath で検索することはできません。「objectElement.getValue()」からテキストを取得し、それを新しい XML のように解析してから、新しい xPath でタグ「id」値を取得できます。また、「objectElement.getValue()」でタグ「id」の値を正規表現で検索することもできます。

于 2013-11-21T15:24:02.427 に答える
0

実際には、JDOM 2.x で新しい XPathAPI を使用する必要があり、pasha701 の回答を考慮に入れると、コードは次のようになります。

Namespace soap = Namespace.getNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
Namespace tempuri = Namespace.getNamespace("turi", ""http://tempuri.org/");
XPathExpression<Element> xpath = XPathFactory.instance().compile(
       "s:Envelope/s:Body/turi:getConfigSettingsResponse/turi:getConfigSettingsResult",
       Filters.element(), null, soap, tempuri);
Element result = xpath.evaluateFirst(documentXML);
String resultxml = result.getValue();
Document resultdoc = new SAXBuilder().build(new StringReader(resultxml));
Element id = resultdoc.getRootElement().getChild("status").getChild("id");
于 2013-11-21T15:47:26.310 に答える