Pregunta

I want to create w3c Document using StAXOMBuilder class in Axiom. And there is a method which can be used to achieve that task.

   OMElement documentElement = new StAXOMBuilder("resources/test.xml").getDocumentElement();
   XMLStreamReader llomReader = documentElement.getXMLStreamReader();
   OMFactory doomFactory = DOOMAbstractFactory.getOMFactory();
   StAXOMBuilder doomBuilder = new StAXOMBuilder(doomFactory, llomReader);

  Document doc = doomBuilder.createDocument(); 

createDocument method is available in STAXOMBuilder Class as a protected method. But when invoked it gives the error "The method createDocument() is undefined for the type StAXOMBuilder "

How to fix this?

¿Fue útil?

Solución 2

The correct way to create a DOM Document instance with Axiom is as follows. First, use OMAbstractFactory#getMetaFactory(String) to get an OMMetaFactory for the Axiom implementation that supports DOM. You do that by passing OMAbstractFactory.FEATURE_DOM to that method. You then have two possibilities:

  1. Cast the OMMetaFactory to DOMMetaFactory and use the JAXP/DOM compatible methods defined by that interface.
  2. Use the Axiom API to create an OMDocument and cast it to a Document. In particular, if you want to parse an existing document, use one of the methods in OMXMLBuilderFactory that takes an OMMetaFactory or OMFactory argument so that Axiom will use the DOM compatible implementation retrieved earlier.

Note that DOOMAbstractFactory is deprecated and that StAXOMBuilder is considered an internal implementation class (as the package name org.apache.axiom.om.impl.builder implies) that should not be used directly.

Otros consejos

Following method worked for me. Thanks to Andreas.

     OMMetaFactory omMetaFactory = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM);
     OMFactory omFac = omMetaFactory.getOMFactory();
     OMXMLParserWrapper wrapper = OMXMLBuilderFactory.createOMBuilder(omFac, new FileInputStream("resources/test.xml")) ;
     Document doc = (Document) wrapper.getDocument();

To be able to invoke a protected method of a class, your class should either subclass StAXOMBuilder or be in the same package.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top