Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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.

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top