1

我正在尝试解析从某个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)

4

1 回答 1

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

}

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

于 2012-07-31T13:16:37.823 回答