Hi Gaurav,
EclipseLink MOXy's org.eclipse.persistence.oxm.XMLDescriptor is a subclass of ClassDescriptor so you will immediately recognize many of its APIs. MOXy's mappings are in the org.eclipse.persistence.oxm.mappings package. They actually extend from DatabaseMapping so you will recognize things like AttributeAccessor and ContainerPolicy.
CustomerProject
Below is what the metadata would look like if created with MOXy's native API. This is very similar to creating EclipseLink's ORM metadata via the native APIs.
package nativeapi;
import org.eclipse.persistence.oxm.XMLDescriptor;
import org.eclipse.persistence.oxm.mappings.*;
import org.eclipse.persistence.sessions.Project;
public class CustomerProject extends Project {
public CustomerProject() {
this.addDescriptor(buildCustomerDescriptor());
this.addDescriptor(buildAddressDescriptor());
this.addDescriptor(buildPhoneNumberDescriptor());
}
private XMLDescriptor buildCustomerDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(Customer.class);
descriptor.setDefaultRootElement("customer");
XMLDirectMapping nameMapping = new XMLDirectMapping();
nameMapping.setAttributeName("name");
nameMapping.setXPath("personal-info/name/text()");
descriptor.addMapping(nameMapping);
XMLCompositeObjectMapping addressMapping = new XMLCompositeObjectMapping();
addressMapping.setAttributeName("address");
addressMapping.setXPath("contact-info/address");
addressMapping.setReferenceClass(Address.class);
descriptor.addMapping(addressMapping);
XMLCompositeCollectionMapping phoneNumbersMapping = new XMLCompositeCollectionMapping();
phoneNumbersMapping.setAttributeName("phoneNumbers");
phoneNumbersMapping.setXPath("contact-info/phone-number");
phoneNumbersMapping.setReferenceClass(PhoneNumber.class);
descriptor.addMapping(phoneNumbersMapping);
return descriptor;
}
private XMLDescriptor buildAddressDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(Address.class);
XMLDirectMapping streetMapping = new XMLDirectMapping();
streetMapping.setAttributeName("street");
streetMapping.setXPath("street/text()");
descriptor.addMapping(streetMapping);
return descriptor;
}
private XMLDescriptor buildPhoneNumberDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(PhoneNumber.class);
XMLDirectMapping valueMapping = new XMLDirectMapping();
valueMapping.setAttributeName("value");
valueMapping.setXPath("text()");
descriptor.addMapping(valueMapping);
return descriptor;
}
}
Demo
Below is some sample code demonstrating how to leverage the metadata to read/write XML.
package nativeapi;
import java.io.File;
import org.eclipse.persistence.oxm.*;
public class Demo {
public static void main(String[] args) {
CustomerProject project = new CustomerProject();
XMLContext xmlContext = new XMLContext(project);
XMLUnmarshaller unmarshaller = xmlContext.createUnmarshaller();
File xml = new File("src/nativeapi/input.xml");
Customer customer = (Customer) unmarshaller.unmarshal(xml);
XMLMarshaller marshaller = xmlContext.createMarshaller();
marshaller.marshal(customer, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<personal-info>
<name>Jane Doe</name>
</personal-info>
<contact-info>
<address>
<street>123 A Street</street>
</address>
<phone-number>555-1111</phone-number>
<phone-number>555-2222</phone-number>
</contact-info>
</customer>
Customer
package nativeapi;
import java.util.List;
public class Customer {
private String name;
private Address address;
private List<PhoneNumber> phoneNumbers;
}
Address
package nativeapi;
public class Address {
private String street;
}
PhoneNumber
package nativeapi;
public class PhoneNumber {
private String value;
}
I will contact you by email to find out more information about what you are trying to build.
-Blaise
[Updated on: Thu, 25 October 2012 19:40]
Report message to a moderator