简体   繁体   English

无法在StAXOMBuilder()类中调用受保护的方法

[英]Can't invoke the protected method in StAXOMBuilder() class

I want to create w3c Document using StAXOMBuilder class in Axiom. 我想使用Axiom中的StAXOMBuilder类创建w3c文档。 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. STAXOMBuilder类中可以使用createDocument方法作为受保护的方法。 But when invoked it gives the error "The method createDocument() is undefined for the type StAXOMBuilder " 但是,当调用它时会出现错误“对于类型为StAXOMBuilder的方法createDocument()未定义”

How to fix this? 如何解决这个问题?

Following method worked for me. 以下方法对我有用。 Thanks to Andreas. 感谢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();

为了能够调用类的protected方法,您的类应该是StAXOMBuilder子类,或者应位于同一包中。

The correct way to create a DOM Document instance with Axiom is as follows. 使用Axiom创建DOM Document实例的正确方法如下。 First, use OMAbstractFactory#getMetaFactory(String) to get an OMMetaFactory for the Axiom implementation that supports DOM. 首先,使用OMAbstractFactory#getMetaFactory(String)为支持DOM的Axiom实现获取OMMetaFactory You do that by passing OMAbstractFactory.FEATURE_DOM to that method. 您可以通过将OMAbstractFactory.FEATURE_DOM传递给该方法来实现。 You then have two possibilities: 然后,您有两种可能性:

  1. Cast the OMMetaFactory to DOMMetaFactory and use the JAXP/DOM compatible methods defined by that interface. OMMetaFactoryDOMMetaFactory并使用该接口定义的JAXP / DOM兼容方法。
  2. Use the Axiom API to create an OMDocument and cast it to a Document . 使用Axiom API创建OMDocument并将其转换为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. 特别是,如果要解析现有文档,请使用OMXMLBuilderFactory中带有OMMetaFactoryOMFactory参数的方法之一,以便Axiom将使用之前检索的DOM兼容实现。

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. 请注意,不赞成使用DOOMAbstractFactory ,并且StAXOMBuilderStAXOMBuilder视为内部实现类(如包名称org.apache.axiom.om.impl.builder所暗示),不应直接使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM