2

私のxmlファイルは次のとおりです。

<xml>
    <group id="1">
        <dstport>8080</dstport>
        <packet id="1">
            <comp type="const">
                <actual-data><![CDATA[GET /]]></actual-data>
                <binary><![CDATA[47 45 54 20 2f ]]></binary>
            </comp>
            <comp type="var">
                <actual-data><![CDATA[host-manager/html HTTP]]></actual-data>
                <binary><![CDATA[68 6f 73 74 2d 6d 61 6e 61 67 65 72 2f 68 74 6d 6c 20 48 54 54 50 ]]></binary>
            </comp>
        </packet>
    </group>
</xml>

タグ内のテキストを取得したいactual-data(この場合は「host-manager/html HTTP」)

私はこれを試しています:

XPathExpression expr = xpath.compile("/xml
                                         /group
                                            /packet
                                               /comp
                                                  /actual-data
                                                     /text()");

しかし、それはヌル文字列を与えています。その[CDATA]のせいでしょうか。私はそれが何であるかを理解していません。それはタグまたは属性ですか?

誰もが値するデータを取得するためのクエリを提供できますか? (この場合は「host-manager/html HTTP」)

4

2 に答える 2

1

2 件の結果があります。したがって、両方のアイテムを元に戻したい場合は、次のようにします。

XPath xpath = XPathFactory.newInstance().newXPath();
String xpathExpression = "/xml/group/packet/comp/actual-data";
InputSource inputSource = new InputSource("test.xml");
NodeList nodes = (NodeList) xpath
    .evaluate(xpathExpression, inputSource, XPathConstants.NODESET);
int j = nodes.getLength();
for (int i = 0; i < j; i++) {
  System.out.println("node:" + nodes.item(i).getTextContent());
}
于 2011-04-19T09:23:02.707 に答える
1

あなたの xpath 式はあいまいです:

このコード:

InputSource inputSource = new InputSource("test.xml");
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("
                              /xml/group/packet/comp/actual-data/text()");
String s = expr.evaluate(inputSource);
System.out.println(s);

表示されます:

GET /

これは、最初の実際のデータ タグの内容です。2 番目のものが必要な場合は、より具体的にする必要があります。

XPathExpression expr = xpath.compile("
                       /xml/group/packet/comp[@type='var']/actual-data/text()");
于 2011-04-19T09:17:20.180 に答える