gpt4 book ai didi

java - 如何检索实际数据标签中的文本?

转载 作者:行者123 更新时间:2023-12-03 16:13:01 25 4
gpt4 key购买 nike

我的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”)

最佳答案

有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());
}

关于java - 如何检索实际数据标签中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5713417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com