为什么我得到一个“DOM 3级未实现”错误在运行时? [英] Why do I get a "DOM Level 3 Not implemented" error at run-time?

查看:366
本文介绍了为什么我得到一个“DOM 3级未实现”错误在运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用XMLBeans绑定而不是默认的JAXB绑定来开发Apache CXF Web服务。我正在使用Java 1.6编译和运行代码。
我在运行时得到以下代码片段的DOM 3级未实现错误:

I am currently working on the development of an Apache CXF Web Service with XMLBeans binding instead of the default JAXB binding. I am compiling and running the code with Java 1.6. I get a "DOM Level 3 Not implemented" error at runtime for the following code snippet :

ExtType[] extTypeList = p.getExtArray();
        for (ExtType extType : extTypeList) {               
            Node node = extType.getDomNode();
            NodeList objList = node.getChildNodes();
            for (int i = 0; i < objList.getLength(); ++i) {
                Node text = (Node) objList.item(i);                 
                if (text.getNodeName() != null
                        && text.getNodeName() == XmlConstant.NODE_NAME) {

                    info.setDuration(text
                            .getTextContent());
                }
            }
        }

JBoss中显示的确切错误如下:

The exact error displayed in JBoss is as follows :

java.lang.RuntimeException: DOM Level 3 Not implemented
        at org.apache.xmlbeans.impl.store.DomImpl._node_getTextContent(DomImpl.java:2516)
        at org.apache.xmlbeans.impl.store.Xobj$NodeXobj.getTextContent(Xobj.java:2607)

从上述错误消息,很明显,getTextContent方法导致exexception,因为在运行时找不到DOM 3级API 。如何消除此错误?我猜想我必须弄清楚哪个jar包含DOM API,并从该jar中删除所有与dom相关的类,以便使用与jdk一起使用的默认DOM API。或者,有没有办法使用DOM来获取xml标签的文本内容,而不依赖于getTextContent方法?

From the above error message, it is clear that the getTextContent method is causing the exexception because the DOM level 3 API's are not found at run-time. How do I eliminate this error? I am guessing I'll have to figure out which jar contains the DOM API's and delete all dom related classes from that jar so that the default DOM API's that come along with jdk are used instead. Alternately, is there a way to get the text contents of an xml tag using DOM without relying on the getTextContent method?

推荐答案

它看起来好像XMLBeans基于异常提供了不符合DOM 3的DOM实现:

It appears as though XMLBeans provides a DOM implementation that is not DOM 3 compliant based on the exception:

java.lang.RuntimeException: DOM Level 3 Not implemented
        at org.apache.xmlbeans.impl.store.DomImpl._node_getTextContent(DomImpl.java:2516)
        at org.a

pache.xmlbeans.impl.store.Xobj$NodeXobj.getTextContent(Xobj.java:2607)

而不是 getTextContent 可以遍历所有子节点,并从文本类型的所有节点附加值。

Instead of getTextContent you could iterate over all the child nodes and append the value from all nodes of type text.

package forum12746038;

import java.io.StringReader;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        String xml = "<root>Hello <!-- comment -->World</root>";
        Document doc = db.parse(new InputSource(new StringReader(xml)));

        Element element = doc.getDocumentElement();
        NodeList childNodes = element.getChildNodes();
        StringBuilder strBldr = new StringBuilder();
        for(int x=0; x<childNodes.getLength(); x++) {
            Node childNode = childNodes.item(x);
            if(childNode.getNodeType() == Node.TEXT_NODE) {
                strBldr.append(childNode.getNodeValue());
            }
        }
        System.out.println(strBldr.toString());
    }

}








我猜想我必须弄清楚哪个jar包含DOM API的
,并从该jar中删除所有与dom相关的类,以便默认的
DOM API使用与jdk一起使用的代码。

I am guessing I'll have to figure out which jar contains the DOM API's and delete all dom related classes from that jar so that the default DOM API's that come along with jdk are used instead.

这可能不会工作,因为我想象XMLBeans返回一个专门的DOM实现,包装在那里自己的对象通过DOM API公开。

This probably won't work as I imagine XMLBeans returns a specialized DOM implementation that wraps there own objects to expose them through DOM APIs.

这篇关于为什么我得到一个“DOM 3级未实现”错误在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆