Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » XmlAdapter with xml-path(Moxy XmlAdapter)
XmlAdapter with xml-path [message #1288789] Tue, 08 April 2014 21:40
Mar Sah is currently offline Mar SahFriend
Messages: 2
Registered: April 2014
Junior Member
I am stuck with trying to use an XmlAdapter in my xml-bindings document to unmarshal xml. The xml looks like this and can't be modified:

<Orders>
<PO>
<POHeader>
<ID>abc</ID>
<VendorCity>Chicago</VendorCity>
<VendorState>IL</VendorState>
<BuyerCity>Topeka</BuyerCity>
<BuyerState>KS</BuyerState>
</POHeader>
</PO>
</Orders>

The classes to be mapped as follows:

public class PO {
String id;
Address vendorAddress;
Address buyerAddress;
}

public class Address {
String city;
String state;
}

Now since multiple tags map to the Address class, I have the following XmlAdapter to handle the buyer Address:

public class BuyerAddressAdapter extends XmlAdapter<BuyerAddressAdapter.BuyerAddress, Address> {

public static class BuyerAddress {
public String BuyerCity;
public String BuyerState;
}

public BuyerAddress marshal(Address v) {
BuyerAddress a = new BuyerAddress();
a.BuyerCity = v.city;
a.BuyerState = v.state;
return a;
}

public Address unmarshal(BuyerAddress v) {
Address a = new Address();
a.city = v.BuyerCity;
a.state = v.BuyerState;
return a;
}
}

Here is the bindings file:

<?xml version="1.0" encoding="US-ASCII"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="ipp.valueobject" xml-mapping-metadata-complete="true" >
<java-types>

<java-type name="Orders" xml-accessor-type="NONE">
<xml-root-element name="Orders" />
<java-attributes>
<xml-elements java-attribute="orders">
<xml-element name="PO" type="ipp.valueobject.PO" />
</xml-elements>
</java-attributes>
</java-type>

<java-type name="PO" xml-accessor-type="NONE">
<java-attributes>
<xml-element java-attribute="id" xml-path="POHeader/ID/text()" />
<xml-element java-attribute="vendorAddress" xml-path="."
type="Address" />

<xml-element java-attribute="buyerAddress" xml-path="." >
<xml-java-type-adapter value="BuyerAddressAdapter" />
</xml-element>
</java-attributes>
</java-type>

</java-types>
</xml-bindings>

When I run the test, the id and vendorAddress fields have the correct values, but the fields in the buyerAddress object are null. I tried changing the xml-path of buyerAddress to be xml-path="POHeader". When I do this, the buyerAddress has the correct values but then the id and vendorAddress fields are null. It is like changing the xml-path causes the other elements to be consumed.

I don't understand what I am doing wrong. Any help is appreciated.

Previous Topic:Insert Duplicate key Exception - Eclipselink
Next Topic:don't get latest data from database
Goto Forum:
  


Current Time: Sat Apr 20 09:42:27 GMT 2024

Powered by FUDForum. Page generated in 0.02787 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top