当前位置: 首页 > 知识库问答 >
问题:

UnmarshalException:意外元素“MDMBroadcast”。预期的元素是“”

尹英华
2023-03-14
MDMBroadcast mdmbroadcast=null;
StringReader reader = new StringReader(broadcastXML);
jaxbContext = JAXBContext.newInstance(com.boots.broadcast.pojo.MDMBroadcast.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

mdmbroadcast = (MDMBroadcast)jaxbUnmarshaller.unmarshal(new StreamSource(reader));

XSD:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="MDMBroadcast" type="MDMBroadcast"/>
   <xsd:complexType name="MDMBroadcast">
      <xsd:sequence>
         <xsd:element name="BroadcastControl" type="BroadcastControl"/>
         <xsd:element name="BroadcastEntity" type="BroadcastEntity" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="BroadcastControl">
      <xsd:all>
         <xsd:element name="transactionName" type="xsd:string" minOccurs="0"/>
         <xsd:element name="transactionId" type="xsd:string" minOccurs="0"/>
         <xsd:element name="transactionCorrelatorId" type="xsd:string" minOccurs="0"/>
         <xsd:element name="externalCorrelationId" type="xsd:string" minOccurs="0"/>
         <xsd:element name="clientSystemName" type="xsd:string" minOccurs="0"/>
         <xsd:element name="clientTransactionName" type="xsd:string" minOccurs="0"/>
         <xsd:element name="sessionId" type="xsd:string" minOccurs="0"/>
         <xsd:element name="requesterName" type="xsd:string" minOccurs="0"/>
         <xsd:element name="requesterLanguage" type="xsd:string" minOccurs="0"/>
         <xsd:element name="requesterLocale" type="xsd:string" minOccurs="0"/>
         <xsd:element name="requestTime" type="xsd:string" minOccurs="0"/>
         <xsd:element name="requesterTimeZone" type="xsd:string" minOccurs="0"/>
         <xsd:element name="requestOrigin" type="xsd:string" minOccurs="0"/>
         <xsd:element name="federatedInstanceName" type="xsd:string" minOccurs="0"/>
         <xsd:element name="customerRequestVersion" type="xsd:string" minOccurs="0"/>
         <xsd:element name="customerEnvironment" type="xsd:string" minOccurs="0"/>
         <xsd:element name="lineOfBusiness" type="xsd:string" minOccurs="0"/>
         <xsd:element name="company" type="xsd:string" minOccurs="0"/>
         <xsd:element name="geographicalRegion" type="xsd:string" minOccurs="0"/>
      </xsd:all>
   </xsd:complexType>
   <xsd:complexType name="BroadcastEntity">
      <xsd:complexContent>
         <xsd:extension base="AbstractBroadcastEntity">
            <xsd:attribute name="entityType" type="xsd:string"/>
         </xsd:extension>
      </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="AbstractBroadcastEntity" abstract="true">
      <xsd:all>
         <xsd:element name="Sources" type="Sources" minOccurs="0"/>
         <xsd:element name="BusinessObject" type="BusinessObject"/>
      </xsd:all>
   </xsd:complexType>
   <xsd:complexType name="Sources">
      <xsd:choice>
         <xsd:element name="System" type="System" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:choice>
   </xsd:complexType>
   <xsd:complexType name="System">
      <xsd:simpleContent>
         <xsd:extension base="xsd:string">
            <xsd:attribute name="key" type="xsd:string"/>
         </xsd:extension>
      </xsd:simpleContent>
   </xsd:complexType>
   <xsd:complexType name="BusinessObject">
      <xsd:complexContent>
         <xsd:extension base="AbstractBusinessObject">
            <xsd:attribute name="name" type="xsd:string"/>
            <xsd:attribute name="performedAction" type="xsd:string"/>
         </xsd:extension>
      </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="AbstractBusinessObject" abstract="true">
      <xsd:sequence>
         <xsd:element name="Attribute" type="BusinessAttribute" minOccurs="0" maxOccurs="unbounded"/>
         <xsd:element name="BusinessObject" type="BusinessObject" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="BusinessAttribute">
      <xsd:simpleContent>
         <xsd:extension base="xsd:string">
            <xsd:attribute name="beforeImage" type="xsd:string"/>
            <xsd:attribute name="name" type="xsd:string"/>
         </xsd:extension>
      </xsd:simpleContent>
   </xsd:complexType>
</xsd:schema>

XML到Unmarshall:

<?xml version="1.0" encoding="UTF-8"?>
<MDMBroadcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="broadcast.xsd">
<BroadcastControl>
<transactionName>persistEntity</transactionName>
<transactionId>323647369125462585</transactionId>
<requesterName>mdmadmin</requesterName>
<requesterLanguage>100</requesterLanguage>
<requesterLocale>en</requesterLocale>
<requesterTimeZone>Europe/London</requesterTimeZone>
</BroadcastControl>
<BroadcastEntity entityType="XPersonBObjExt">
<Sources>
</Sources>
<BusinessObject name="XPersonBObjExt" performedAction="update">
<Attribute name="PersonLastUpdateDate" beforeImage="2016-09-12 15:33:54.49">2016-09-12 15:40:55.243</Attribute>
<Attribute name="PersonLastUpdateTxId" beforeImage="534747369083443461">969047369125518571</Attribute>
<Attribute name="XPersonLastUpdateDate" beforeImage="2016-09-12 14:33:54.49"></Attribute>
<Attribute name="XPersonLastUpdateUser" beforeImage="mdmadmin"></Attribute>
<Attribute name="EntityStatusType" beforeImage="">1</Attribute>
<Attribute name="PartyLastUpdateDate" beforeImage="2016-09-12 15:33:54.481">2016-09-12 15:40:55.236</Attribute>
<Attribute name="PartyLastUpdateTxId" beforeImage="534747369083443461">969047369125518571</Attribute>
<Attribute name="BirthDate" beforeImage="1956-08-10 00:00:00.0">1978-08-10 00:00:00.0</Attribute>
<BusinessObject name="TCRMPartyAddressBObj" performedAction="update">
<BusinessObject name="XAddressBObjExt" performedAction="update">
<Attribute name="XNoFixedAbodeFlag" beforeImage="Y"></Attribute>
<Attribute name="XAddressLastUpdateUser" beforeImage="mdmadmin"></Attribute>
<Attribute name="XGoneAway" beforeImage="N"></Attribute>
<Attribute name="XLocality" beforeImage="Kolkata"></Attribute>
<Attribute name="XAddressLastUpdateDate" beforeImage="2016-09-12 11:10:27.81"></Attribute>
<Attribute name="XAddressLastUpdateTxId" beforeImage="273847367862778699"></Attribute>
</BusinessObject>
</BusinessObject>
</BusinessObject>
</BroadcastEntity>
</MDMBroadcast>

共有1个答案

吴星汉
2023-03-14

如果生成的JAXB类MDMBroadcast没有定义@xmlrootelement,那么解封程序可能无法直接执行此操作。您可以使用JaxBelement 进行访问。但是我建议,如果您可以像下面这样更改模式(.xsd),那么JAXB将自动生成@xmlRootelement,并且您的解封方式将照样工作。

在这个链接中解释了xjc默认不生成@xmlrootelement的原因

更改:根元素的复杂类型是内联定义的

<xsd:element name="MDMBroadcast">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="BroadcastControl" type="BroadcastControl" />
            <xsd:element name="BroadcastEntity" type="BroadcastEntity"
                minOccurs="0" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
jaxbContext = JAXBContext.newInstance("com.boots.broadcast.pojo");
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<MDMBroadcast> mdmbroadcast = (JAXBElement <MDMBroadcast>)jaxbUnmarshaller.unmarshal(new StreamSource(reader));

MDMBroadcast obj = mdmbroadcast.getValue();
 类似资料: