错误:java.lang.String 无法转换为 org.w3c.dom.Node
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9712879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: java.lang.String cannot be cast to org.w3c.dom.Node
提问by Asdfg
I am trying to parse xml string and I am getting java.lang.String cannot be cast to org.w3c.dom.Node
error.
我正在尝试解析 xml 字符串,但java.lang.String cannot be cast to org.w3c.dom.Node
出现错误。
This is the code I am using:
这是我正在使用的代码:
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
String expression = "//Home/ListOfCustomers";
XPathExpression xPathExpression = xPath.compile(expression);
Object nl = xPathExpression.evaluate(xmlResp);
This is how the XML string is constructed:
这是 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>
What am I missing here?
我在这里缺少什么?
采纳答案by Jasonw
Object nl = xPathExpression.evaluate(xmlResp);
对象 nl = xPathExpression.evaluate(xmlResp);
This is the issue here. With a single argument to the evaluatemethod, it expect either variable of type InputSource or Object, have you declare xmlResp to either of it? Also, both of these methods return of type String, so why are you assign to a variable of type Object?
这是这里的问题。对于评估方法的单个参数,它期望 InputSource 或 Object 类型的变量,您是否将 xmlResp 声明为其中之一?此外,这两种方法都返回 String 类型,那么为什么要分配给 Object 类型的变量呢?
Since you have the xml file, why don't you initialize your xmlResp to type of InputSource? Then use the xPathExpression evaluate on the inputsource? Something like the following.
既然您有 xml 文件,为什么不将您的 xmlResp 初始化为 InputSource 类型?然后在输入源上使用 xPathExpression 评估?类似于以下内容。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class XMLParser
{
/**
* @param args
*/
public static void main(String[] args)
{
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
InputSource doc = new InputSource(new InputStreamReader(new FileInputStream(new File("file.xml"))));
String expression = "//Home/ListOfCustomers";
XPathExpression xPathExpression = xPath.compile(expression);
NodeList elem1List = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
xPathExpression = xPath.compile("@type");
for (int i = 0; i < elem1List.getLength(); i++)
{
System.out.println(xPathExpression.evaluate(elem1List.item(i), XPathConstants.STRING));
}
}
catch (XPathExpressionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
回答by Matt Harrison
Just having a quick look at the docs: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/xpath/XPathExpression.html#evaluate(java.lang.Object)
只需快速浏览一下文档:http: //docs.oracle.com/javase/1.5.0/docs/api/javax/xml/xpath/XPathExpression.html#evaluate(java.lang.Object)
Then the api defines this for compile: item - The starting context (node or node list, for example).
然后 api 为编译定义了这个: item - 起始上下文(例如节点或节点列表)。
So assuming this is the method you are using, it looks like you need to send in a node - or node list, not just a String.
因此,假设这是您正在使用的方法,看起来您需要发送一个节点 - 或节点列表,而不仅仅是一个字符串。