首页 > 解决方案 > JAXB 解组无法返回具有替代组的元素

问题描述

在解组 xml 文档时,JAXB 无法解组具有替换组名称的复杂类型。该文档带有替代名称,并且 JAXB 解组过程无法将数据与实际元素绑定。最终我得到一个空对象,其数据到达 xml 文档。这是 JAXB 中的错误还是我在这里做错了什么?

注意:下面的模式清楚地定义了有问题的复杂元素类型 ENBFunction 的替代名称。解组“xn:ManagedElementOptionallyContainedNrmClass”时没有错误,但Java类“ENBFunction”中的数据未填充。

    <element name="ENBFunction" substitutionGroup="xn:ManagedElementOptionallyContainedNrmClass">
    <complexType>
      <complexContent>
        <extension base="xn:NrmClass">
          <sequence>
            <element name="attributes" minOccurs="0">
              <complexType>
                <all>
                  <element name="userLabel" type="string"/>
                  <element name="enbId" type="en:EnbId" minOccurs="0"/>
                  <element name="x2BlackList" type="xn:dnList" minOccurs="0"/>
                  <element name="x2WhiteList" type="xn:dnList" minOccurs="0"/>
                  <element name="x2HOBlackList" type="xn:dnList" minOccurs="0"/>
                  <element name="x2IpAddressList" type="string" minOccurs="0"/>
                  <element name="tceIDMappingInfoList" type="en:TceIDMappingInfoList" minOccurs="0"/>
                  <!-- linkList attribute is to be added when defined in the IS -->
                </all>
              </complexType>
            </element>
            <choice minOccurs="0" maxOccurs="unbounded">
              <element ref="en:EUtranCellFDD"/>
              <element ref="en:EUtranCellTDD"/>
              <element ref="epc:EP_RP_EPS"/>
              <element ref="en:ENBFunctionOptionallyContainedNrmClass"/>
              <element ref="en:DeNBCapability"/>
              <element ref="xn:VsDataContainer"/>
            </choice>
            <choice minOccurs="0" maxOccurs="1">
              <element ref="sp:ESPolicies"/>
            </choice>
            <choice minOccurs="0" maxOccurs="1">
              <element ref="sp:SONControl"/>
            </choice>
            <choice minOccurs="0" maxOccurs="1">
              <element ref="sp:SONTargets"/>
            </choice>
          </sequence>
        </extension>
      </complexContent>
    </complexType>
  </element>

标签: jaxbunmarshallingsubstitution

解决方案


主要问题在于我创建 JAXBContext 实例的方式。JAXB 提供了不止一种创建 JAXBContext 实例的方法:

static JAXBContext  newInstance(Class... classesToBeBound) 
       Obtain a new instance of a JAXBContext class.
static JAXBContext  newInstance(Class[] classesToBeBound, Map<String,?> properties) 
       Obtain a new instance of a JAXBContext class.
***static JAXBContext   newInstance(String contextPath) 
       Obtain a new instance of a JAXBContext class.***
static JAXBContext  newInstance(String contextPath, ClassLoader classLoader) 
       Obtain a new instance of a JAXBContext class.
static JAXBContext  newInstance(String contextPath, ClassLoader classLoader, Map<String,?> properties) 
       Obtain a new instance of a JAXBContext class.

最初我使用的是第一个,

static JAXBContext  newInstance(Class... classesToBeBound) 

我在其中提供了所有要绑定的 jaxb 类,但不知何故不起作用。当我使用第三种对象创建方式时,它起作用了,它采用一个字符串,该字符串具有冒号分隔的包名称,所有 jaxb 类都驻留在其中。

static JAXBContext   newInstance(String contextPath)

推荐阅读