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

时间:2012-07-31 04:19:58

标签: java xml xpath clob

我正在尝试解析从Clob类型获取的XML数据。出于某种原因,我得到了这个例外......我无法弄清楚为什么。我从包含XMLType值的oracle数据库列中获取了Clob,即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无法强制转换为   org.w3c.dom.Node at   com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(未知   来源)at   com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(未知   来源)at   com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(未知   来自)helper.XPathReader.read(XPathReader.java:34)at   helper.XmlHelper.xPathRead(XmlHelper.java:325)

1 个答案:

答案 0 :(得分:1)

无论如何你不能从字符串中做到这一点。

这样的事情

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;
}

}

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