我正在尝试解析从某个Clob
类型获得的 XML 中的数据。出于某种原因,我得到了这个例外......我不知道为什么。我从包含一个XMLType
值的 Oracle 数据库列中获得了 Clob,即 XML。我已经成功地将包含的 XML 提取XMLType
到一个Clob
类型中。这是通过以下代码获得的:
xmlBean = XmlHelper.xPathRead(film.getXmlClob());
film
如果您愿意,该对象在模型中,并且该getXmlClob()
方法返回一个Clob
包含我想使用 XPath 解析的 XML 的值。这是其余的代码:
XML 助手
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 无法转换为 com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(未知来源)的 com.sun.org 的 org.w3c.dom.Node .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 ) 在 helper.XmlHelper.xPathRead(XmlHelper.java:325)