我的解封代码:
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>
如果生成的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();
XSD: XML到Unmarshall:
我在解组字符串时遇到了这个错误。我已经使用JAXB创建了Java文件。 输入字符串:
重要提示:我想说明我已经读过javax.xml.bind.UnmarshalException:意外元素。预期的元素是(无),并尝试了建议的解决方案,但没有成功。 我尝试将反序列化为用生成的类。这是代码: 这是一个错误: UnMarshalException:意外元素(URI:“”,local:“id”)。所需元素为(无) 这是原始的: 我使用的是8(Azul ZuluFx 8.0.202)和2.
在这里,我将从XML创建java对象:
有几个关于这个问题的帮助主题,但我还没有找到一个解决我的问题的解决方案。我很欣赏在解决这个问题上的指导。 例外情况
我知道有很多像这样的问题,但没有一个给我提供正确的答案,所以我来这里。 下面是我得到的XML: 下面是相关的java类: