gpt4 book ai didi

java - 错误 : java. lang.String 无法转换为 org.w3c.dom.Node

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

我正在尝试解析 xml 字符串,但得到 java.lang.String cannot be cast to org.w3c.dom.Node错误。

这是我正在使用的代码:

        XPathFactory xPathFactory = XPathFactory.newInstance();

XPath xPath = xPathFactory.newXPath();

String expression = "//Home/ListOfCustomers";

XPathExpression xPathExpression = xPath.compile(expression);

Object nl = xPathExpression.evaluate(xmlResp);

这是 XML 字符串的构造方式:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Home>
<ListOfCustomers type="Regular" count="939">
<Customer>
<CustName>xyz</CustName>
</Customer>
<Customer>
<CustName>abc</CustName>
</Customer>
<Customer>
<CustName>def</CustName>
</Customer>
</ListOfCustomers>
</Home>

我在这里缺少什么?

最佳答案

只需快速浏览一下文档:
http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/xpath/XPathExpression.html#evaluate(java.lang.Object )

然后 api 为编译定义了这个:
item - 起始上下文(例如节点或节点列表)。

因此,假设这是您正在使用的方法,看起来您需要发送一个节点 - 或节点列表,而不仅仅是一个字符串。

关于java - 错误 : java. lang.String 无法转换为 org.w3c.dom.Node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9712879/

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