我是XML编组的新手,需要帮助和指导,如何使用JAXB和groovy在XML之下进行编组。我需要创建类,但不确定如何在xml:lang字段的这一部分中读取AKJ图书的值。
谢谢您的帮助。
<cXML payloadID="20190205180826780.ShipNotice@Ariba.AKJBooks.com" xml:lang="en-US" timestamp="2019-02-05T18:08:25-05:00">
<Header>
<From>
<Credential domain="NetworkId">
<Identity>AN01001332601</Identity>
</Credential>
</From>
<To>
<Credential domain="NetworkId">
<Identity>AN01001160200</Identity>
</Credential>
</To>
<Sender>
<Credential domain="NetworkId">
<Identity>AN01001332601</Identity>
<SharedSecret>XXXXXX</SharedSecret>
</Credential>
<UserAgent>AKJBooks Ariba Interface</UserAgent>
</Sender>
</Header>
<Request deploymentMode="production">
<ShipNoticeRequest>
<ShipNoticeHeader shipmentID="A1464251-426A-4825-80F5-1C85FB530904" noticeDate="2019-02-05T18:06:22-05:00" shipmentDate="2019-02-04T00:00:00-05:00" deliveryDate="2019-02-11T00:00:00-05:00">
<Contact role="shipFrom">
<Name xml:lang="en-US">AKJ Books</Name>
<PostalAddress>
<Street>4702 Benson Avenue</Street>
<City>Halethorpe</City>
<State>MD</State>
<PostalCode>21227</PostalCode>
<Country isoCountryCode="US">USA</Country>
</PostalAddress>
<Phone>
<TelephoneNumber>
<CountryCode isoCountryCode="US">1</CountryCode>
<AreaOrCityCode>410</AreaOrCityCode>
<Number>2421602</Number>
</TelephoneNumber>
</Phone>
</Contact>
<Comments xml:lang="en-US" />
</ShipNoticeHeader>
<ShipControl>
<CarrierIdentifier domain="SCAC">FDE</CarrierIdentifier>
<CarrierIdentifier domain="companyName">Federal Express</CarrierIdentifier>
<ShipmentIdentifier>476352763792</ShipmentIdentifier>
</ShipControl>
<ShipNoticePortion>
<OrderReference orderID="J024157938793101">
<DocumentReference payloadID="1548443458012.1408509556.OrderRequest@DonorsChoose.org" />
</OrderReference>
<ShipNoticeItem quantity="6" lineNumber="1">
<UnitOfMeasure>EA</UnitOfMeasure>
</ShipNoticeItem>
<ShipNoticeItem quantity="6" lineNumber="2">
<UnitOfMeasure>EA</UnitOfMeasure>
</ShipNoticePortion>
</ShipNoticeRequest>
</Request>
</cXML>
答案 0 :(得分:0)
首先,您的XML无效。末尾缺少结束符</ShipNoticeItem>
。解决此问题后,
import groovy.util.XmlSlurper
def xml = '''<cXML payloadID="20190205180826780.ShipNotice@Ariba.AKJBooks.com" xml:lang="en-US" timestamp="2019-02-05T18:08:25-05:00">
<Header>
<From>
<Credential domain="NetworkId">
<Identity>AN01001332601</Identity>
</Credential>
</From>
<To>
<Credential domain="NetworkId">
<Identity>AN01001160200</Identity>
</Credential>
</To>
<Sender>
<Credential domain="NetworkId">
<Identity>AN01001332601</Identity>
<SharedSecret>XXXXXX</SharedSecret>
</Credential>
<UserAgent>AKJBooks Ariba Interface</UserAgent>
</Sender>
</Header>
<Request deploymentMode="production">
<ShipNoticeRequest>
<ShipNoticeHeader shipmentID="A1464251-426A-4825-80F5-1C85FB530904" noticeDate="2019-02-05T18:06:22-05:00" shipmentDate="2019-02-04T00:00:00-05:00" deliveryDate="2019-02-11T00:00:00-05:00">
<Contact role="shipFrom">
<Name xml:lang="en-US">AKJ Books</Name>
<PostalAddress>
<Street>4702 Benson Avenue</Street>
<City>Halethorpe</City>
<State>MD</State>
<PostalCode>21227</PostalCode>
<Country isoCountryCode="US">USA</Country>
</PostalAddress>
<Phone>
<TelephoneNumber>
<CountryCode isoCountryCode="US">1</CountryCode>
<AreaOrCityCode>410</AreaOrCityCode>
<Number>2421602</Number>
</TelephoneNumber>
</Phone>
</Contact>
<Comments xml:lang="en-US" />
</ShipNoticeHeader>
<ShipControl>
<CarrierIdentifier domain="SCAC">FDE</CarrierIdentifier>
<CarrierIdentifier domain="companyName">Federal Express</CarrierIdentifier>
<ShipmentIdentifier>476352763792</ShipmentIdentifier>
</ShipControl>
<ShipNoticePortion>
<OrderReference orderID="J024157938793101">
<DocumentReference payloadID="1548443458012.1408509556.OrderRequest@DonorsChoose.org" />
</OrderReference>
<ShipNoticeItem quantity="6" lineNumber="1">
<UnitOfMeasure>EA</UnitOfMeasure>
</ShipNoticeItem>
<ShipNoticeItem quantity="6" lineNumber="2">
<UnitOfMeasure>EA</UnitOfMeasure>
</ShipNoticeItem>
</ShipNoticePortion>
</ShipNoticeRequest>
</Request>
</cXML>
'''
def parsed = new XmlSlurper().parseText xml
println parsed.Request.ShipNoticeRequest.ShipNoticeHeader.Contact.Name.text()
==> AKJ Books
您可以使用XmlSlurper解析xml,然后沿着树走到所需的节点。