gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:08:09 26 4
gpt4 key购买 nike

我正在尝试解析从 Clob 类型获取的 XML 数据。由于某种原因,我遇到了这个异常......我不明白为什么。我从 Oracle 数据库列中获取了 Clob,其中包含 XMLType 值,即 XML。我已成功将 XMLType 中包含的 XML 提取为 Clob 类型。这是通过以下代码获得的:

xmlBean =  XmlHelper.xPathRead(film.getXmlClob());

如果您愿意的话,film 对象位于模型中,并且 getXmlClob() 方法返回一个 Clob 值,其中包含我想要使用 XPath 解析的 XML。这是其余的代码:

XMLHelper

public static XmlBean xPathRead(Clob xmlClob) {
XPathReader reader = new XPathReader(xmlClob);
XmlBean xmlBean = new XmlBean(); // just an entity from the model to store the xml's contents inside

String filmId = "/IMAGE[1]/@id";
xmlBean.setFilmId(Integer.parseInt((String) reader.read(filmId, XPathConstants.STRING)));
}

XPathReader

public class XPathReader {
private String xmlString;
private XPath xPath;

public XPathReader(Clob xmlClob) {

try {
if ((int) xmlClob.length() > 0) {
String s = xmlClob.getSubString(1, (int) xmlClob.length()); // this is a way to cast a CLOB into a STRING
this.xmlString = s;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
xPath = XPathFactory.newInstance().newXPath();
}

public Object read(String expression, QName returnType) {
try {
XPathExpression xPathExpression = xPath.compile(expression);
return xPathExpression.evaluate(xmlString, returnType);
} catch (XPathExpressionException ex) {
ex.printStackTrace();
return null;
}
}
}

有什么想法吗?问题出在 evaluate() 方法中...

错误消息

java.lang.ClassCastException: java.lang.String cannot be cast to org.w3c.dom.Node at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(Unknown Source) at helper.XPathReader.read(XPathReader.java:34) at helper.XmlHelper.xPathRead(XmlHelper.java:325)

最佳答案

无论如何你都不能通过字符串来做到这一点。

像这样怎么样

public class XPathReader {
private XPath xPath;
private Document doc;

public XPathReader(Clob xmlClob) {

try {
if ((int) xmlClob.length() > 0) {
String s = xmlClob.getSubString(1, (int) xmlClob.length()); // this is a way to cast a CLOB into a STRING
doc = readDoc(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
xPath = XPathFactory.newInstance().newXPath();
}

public Object read(String expression, QName returnType) {
try {
XPathExpression xPathExpression = xPath.compile(expression);
return xPathExpression.evaluate(doc, returnType);
} catch (XPathExpressionException ex) {
ex.printStackTrace();
return null;
}
}

public Document readDoc(String s)
{
Document d = null;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(s));
d = builder.parse(is);
} catch (Exception e)
{
}
return d;
}

}

我还没有真正尝试过,你需要更好的错误处理,但它应该可以正常工作

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

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