我们可以使用JAXB将XSD转换为Java Object。如果XML有多个元素,则会从中创建多个类。 JAXB创建了多个用于跟踪XML的类:
<cXML payloadID="dfbgw859yjdfngoho9gj" timestamp="12/12/2012 4:51:22 PM" xml:lang=" en-us ">
<Header>
<From>
<Credential domain="web">
<Identity>UAT Master</Identity>
</Credential>
</From>
<To>
<Credential domain="web">
<Identity>notused</Identity>
</Credential>
</To>
<Sender>
<Credential domain="web">
<Identity>notused</Identity>
</Credential>
</Sender>
</Header>
<Request>
<OrderRequest>
<OrderRequestHeader orderDate="12/12/2012 4:51:22 PM" type="new">
<ID domain="Web">645</ID>
<Total>
<Money currency="USD">129.48</Money>
</Total>
<Shipping>
<Money currency="USD">12.57</Money>
<Description xml:lang="en-US"/>
</Shipping>
<Tax>
<Money currency="USD">8.91</Money>
</Tax>
<Extrinsic name="Additional Comments or Instructions:"/>
<BillTo>
<Address contactId="210306">
<CompanyName>UAT Master</CompanyName>
<ContactId domain="Web">210306</ContactId>
<PaymentType>CREDITCARD</PaymentType>
<PostalAddress name="default">
<FirstName>test</FirstName>
<LastName>test</LastName>
<Street>9050 Malabar Hills</Street>
<City>Newark</City>
<State>NJ</State>
<Country isoCountryCode="USA">USA</Country>
<PostalCode>40485</PostalCode>
<Email>test@test.com</Email>
<WorkPhone>2342423434</WorkPhone>
</PostalAddress>
</Address>
</BillTo>
<Extrinsic name="Purchase Order Number:"/>
<Contact Role="buyer">
<ID domain="Web">testName</ID>
<Name>testName M</Name>
<Email>testName.testLastName@test.com</Email>
<Company>UAT Master</Company>
<Department/>
</Contact>
</OrderRequestHeader>
<ItemOut quantity="120" lineNumber="1">
<Type>PRINT</Type>
<ItemId domain="Web">210300</ItemId>
<StatusType>APPROVED</StatusType>
<PostageAmount currency="USD">0.00</PostageAmount>
<Discount currency="USD">0.00</Discount>
<orderItemName>cxml test for cc payment</orderItemName>
<orderItemDescription>test cxml only with cc payment</orderItemDescription>
<Price>108.00</Price>
<ItemID>
<SupplierPartID>BUSCRD001</SupplierPartID>
<SupplierPartAuxiliaryID/>
</ItemID>
<ItemDetail>
<URL>
http://127.0.1.1/Agggf/NgA=/Mfsdfsf==/2012/3/28/dfafDYAMgA1AA==.jpg
</URL>
<Description xml:lang="en-US">Business Card</Description>
<UnitPrice>
<Money currency="USD">0.90</Money>
</UnitPrice>
<UnitOfMeasure>PER_PIECE</UnitOfMeasure>
<Extrinsic name="options">
<ul>
<li>
Color/Yellow=
<span>0.00</span>
</li>
<li>
Paper Type/Gloss=
<span>0.00</span>
</li>
</ul>
</Extrinsic>
<Classification domain=""/>
<ManufacturerPartID/>
<ManufacturerName xml:lang="en-US"/>
</ItemDetail>
<ShipTo>
<Address contactId="210301">
<ContactId domain="Web">210301</ContactId>
<CompanyName>UAT Master</CompanyName>
<PostalAddress name="default">
<FirstName>test</FirstName>
<LastName>test</LastName>
<Street>9050 Great Hills</Street>
<City>Newark</City>
<State>New Jersey</State>
<Country isoCountryCode="USA">USA</Country>
<PostalCode>78759</PostalCode>
<Email>testName.testLastName@test.com</Email>
<WorkPhone>2342423434</WorkPhone>
</PostalAddress>
</Address>
</ShipTo>
<Shipping>
<Money currency="USD">12.57</Money>
<Description xml:lang="en-US">USPS Ground</Description>
</Shipping>
<Distribution>
<Accounting name="DistributionCharge">
<AccountingSegment id="">
<Name xml:lang="en-US"/>
<Description xml:lang="en-US"/>
</AccountingSegment>
<AccountingSegment id="">
<Name xml:lang="en-US"/>
<Description xml:lang="en-US"/>
</AccountingSegment>
</Accounting>
<Charge>
<Money currency="USD"/>
</Charge>
</Distribution>
<Distribution>
<Accounting name="DistributionCharge">
<AccountingSegment id="">
<Name xml:lang="en-US"/>
<Description xml:lang="en-US"/>
</AccountingSegment>
<AccountingSegment id="">
<Name xml:lang="en-US"/>
<Description xml:lang="en-US"/>
</AccountingSegment>
</Accounting>
<Charge>
<Money currency="USD"/>
</Charge>
</Distribution>
<Comments xml:lang="en-US"/>
</ItemOut>
</OrderRequest>
</Request>
</cXML>
我必须编写一个REST服务,它可以接受带有多个元素的xml。我成功地接受了一个简单的xml,因为它只有单个类。 如何实现这一点。
@POST
@Path("/XMLTest")
@Consumes("application/xml")
public Response consumeXML(**WhichObjectToAcceptHere whichObject**) {
String output = objectFactory.toString();
return Response.status(200).entity(output).build();
}