Why do I get a “DOM Level 3 Not implemented” error at run-time?

冷暖自知 提交于 2019-12-02 09:08:10

问题


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

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)

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?


回答1:


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)

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

}

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.

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.




回答2:


Well according to this page, Java 6's XML support is supposed to be DOM 3 compliant. See also the Java SE 6 javadoc.

This leads me to think that your application when you run it on JBoss must have older XML jars on the classpath, or something like that.




回答3:


FYI: this is often caused by maven or something adding very old xml-apis or xmlParserApis jars to the classpath, usually via xerces dependencies, these can be removed, Java has had XML built in for ages. Modern Java code should never depend on xerces.



来源:https://stackoverflow.com/questions/12746038/why-do-i-get-a-dom-level-3-not-implemented-error-at-run-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!