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>
如果生成的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();
我的解封代码: XSD: 要取消组播得XML:
你可以在这个网站找到很多这样的问题,但没有一个解决了我的问题。 这是我的XML: 我已经使用XJC编译器创建了Jaxb类,创建的文件如下所示:
然后得到上面发布的错误。为什么会这样?我的代码错了吗?
我在将XML响应从服务转换为POJO时遇到一个异常。XML如下所示: 我是这样用的: 下面是我的文件的详细信息 包-info.java ItemLink.java ItemLinks.java
我在解组字符串时遇到了这个错误。我已经使用JAXB创建了Java文件。 输入字符串: