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

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:

<?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,并且您的解组方式可以按原样工作。

并且@XmlRootElement默认情况下不由xjc生成的原因将在本链接中解释

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

<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();
 类似资料: