Given a scenario:
I have my own system's object structure. Now there are more than one XML sources I have to map to my java classes. And there is no need to convert Java object back into XML.
What's your suggestion for me to use Digester or JAXB? Currently I lean to use Digester, because I can specify XML path for each XML source to the same object method call, and Digester seems to be easier to maintain. Although JAXB has good design to marshal/unmarshal java and XML, but I think it is too complicated, xml schema is needed for each xml-java mapping, right?
I think both Digester or JAXB has their mission to fit different usage scenario, so need your advice to help me decide one of them. Thanks a lot.
I think you may have a skewed view of JAXB. It can be complicated, if you choose to make it so, but it can also be extremely simple. For example, you can bind an entire XML document onto an object graph with only a single annotation.
Also, the schema thing is a red herring. JAXB can generate java code from an XML Schema, but that's just a convenience for cases where you have a schema. If you don't, then ignore that part. You can annotate your class model by hand, it's very easy.
Digester, on the other hand, is harder to maintain (in my opinion), since you have to muck about with path expressions ion addition to your class model.
An advantage of JAXB is that it is a spec (JSR-222) with multiple implementations: Metro, EclipseLink MOXy, JaxMe. This avoids the problem of vendor lock in.
XPath Based Mapping
The EclipseLink JAXB (MOXy) has an extension to support XPath based mapping (I'm the tech lead).
package blog.geocode;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="kml")
#XmlType(propOrder={"country", "state", "city", "street", "postalCode"})
public class Address {
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()")
private String street;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:LocalityName/text()")
private String city;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()")
private String state;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()")
private String country;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()")
private String postalCode;
}
Multiple XML Sources
To apply multiple XML representations to an object model you can leverage MOXy's XML metadata. This is another extension to the JAXB standard. An example file looks like:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="blog.bindingfile">
<xml-schema
namespace="http://www.example.com/customer"
element-form-default="QUALIFIED"/>
<java-types>
<java-type name="Customer">
<xml-root-element/>
<xml-type prop-order="firstName lastName address phoneNumbers"/>
<java-attributes>
<xml-element java-attribute="firstName" name="first-name"/>
<xml-element java-attribute="lastName" name="last-name"/>
<xml-element java-attribute="phoneNumbers" name="phone-number"/>
</java-attributes>
</java-type>
<java-type name="PhoneNumber">
<java-attributes>
<xml-attribute java-attribute="type"/>
<xml-value java-attribute="number"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
For more information:
http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html
Maybe off topic: I've abandonded digester in favor of xstream. Maybe have a look
Related
I have a couple of java classes that I want to convert to xml using jaxb. (I have no need to generate the classes based on the schema) I need to be able to map the class to different xml formats so I do not want to use annotations. From what I've seen my best option seems to be to use external xml bindings. So I wanted to know:
1) I am using eclipse. I am new to JAXB and I would like to know how to integrate external bindings using eclipse?
2) What other options other than external xml bindings are available?
I think you best option is to use MOXy XML bindings:
http://www.eclipse.org/eclipselink/documentation/2.6/moxy/runtime003.htm
This allows you to define XML<->Java mappings in form of XML files instead of annotations:
<?xml version="1.0" encoding="US-ASCII"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="example">
<java-types>
<java-type name="Customer">
<xml-root-element name="customer-info" />
<java-attributes>
<xml-attribute java-attribute="custId" name="customer-id" />
<xml-element java-attribute="picture" name="picture-hex">
<xml-schema-type name="hexBinary" />
<xml-java-type-adapter
value="example.adapters.MyHexConverter" />
</xml-element>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
You can use this file via JAXBContextProperties.OXM_METADATA_SOURCE property:
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
JAXBContext ctx = JAXBContext.newInstance(new Class[] { Customer.class }, properties);
So if you want several different mappings for the same class, just write several XML bindings and create your JAXBContext with corresponding files. I think this is the best options right now, with MOXy
With pure JAXB RI you can write an own annotations reader. I did this once with Annox:
http://confluence.highsource.org/display/ANX/JAXB+User+Guide
Another option was JBoss JAXBIntroductions, also based on a custom annotations reader:
https://developer.jboss.org/wiki/JAXBIntroductions
But I'm not sure this is live anymore.
Since you want multiple mappings, you'll have to write them (all but one) manually. You can generate one set of mappings as annotations, but further mappings will have to be written manually. Or, let's say, I'm not aware of a plugin or tool which would generate, for instance, MOXy XML bindings. Wouldn't be a big problem to write one though.
You may also take a completely different approach. Instead of mapping one central model with different mappings/format, you can have map a separate package of DTOs onto these formats. And then convert between your DTOs and the central model. Something like
XML(1) <-> DTO(1)|<-\
XML(2) <-> DTO(2)|<--*->Model
XML(3) <-> DTO(3)|<-/
Thus you'll have clean DTOs per exchange format (which you can generate out of schemas) and a single central business model (to rule them all). You'll have to convert between DTOs and the Model, this can be handled with a tool like Dozer.
If this is a better approach or not depends on how complex your formats are and how different they are fron one another.
Concerning your questions:
1) There's nothing special about Eclipse, just add MOXy as dependency and follow the docs.
2) I've described a few options above.
See "Passing Customization Files to the JAXB Binding Compiler" here:
https://docs.oracle.com/javase/tutorial/jaxb/intro/custom.html
You could write a script to integrate into Eclipse. Or you could use a Maven or Ant task.
Your only options are annotations or XML bindings.
i have one question. What is the best way to serialize in java 1 object to few xml with different schema? For example we have this java bean:
class User {
String name;
String gender;
String age;
}
And i want to serialize it to first
<user>
<name>bobby</name>
<gender>male</gender>
<age>40</age>
</user>
second
<info>
<employer>
<data>
<name>bobby</name>
</data>
<meta>
<gender>male</gender>
<age>40</age>
</meta>
</employer>
</info>
and maybe another one xml. What you think?
You could refer to this answer. The JAXB lib generate the serialization xml file based on a .xsd file. And of course you can customize the .xsd file for your own purpose.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
Metadata Representation
Generally object-to-XML providers offer different ways of specifying metadata. Annotations is one way of specifying metadata and in all instances of seen annotations correspond to only one representation. So you need to look for a provider that offers an alternate approach such as an XML mapping document or programmatic metadata.
Path Based Mapping
Generally object-to-XML providers offer mapping through a 1-to-1 relationship between objects/properties and levels of nesting. To map to your second representation you need a provider that is able to do path based mapping.
MOXy offers both items that you are looking for. Below is a link to an example where a single object model is mapped to both the Google and Yahoo weather APIs:
http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html
Currently our software is interacting with a remote web service via XML/SOAP. We're using XSDs to conveniently create the requests to be sent to the remote system. That's fine, and it's working well. XSD is pretty awesome.
However, it does lead to very lengthy code blocks for creating the Document objects. I'm wondering if anyone has suggestions for good design practices to handle these large code blocks for creation of the request and handling of the response Documents.
The point of the XSD Document object is to decouple the XML creation from the client. However, I'm thinking a facade class might be a good idea for each service. I think this would be great for creation, since the facade class could be given the ingredients to build the request document, and send the request, without coupling the client to the XSD classes. I think a problem arises in the response Documents. If there is a multi-level response, you'll end up creating POJO classes just to wrap the XSD classes, which seems like over-kill.
Note: I'm the EclipseLink JAXB (MOXy) tech lead.
EclipseLink JAXB (MOXy)
MOXy is a JAXB (JSR-222) implementation that has an XPath based mapping extension. This means you can map a more compact object model to your XML. In the example below a simple Address object is mapped to Google's Geocoding V2 format:
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="kml")
#XmlType(propOrder={"country", "state", "city", "street", "postalCode"})
public class Address {
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()")
private String street;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:LocalityName/text()")
private String city;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()")
private String state;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()")
private String country;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()")
private String postalCode;
}
The above class corresponds to the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0" xmlns:ns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
<Response>
<Placemark>
<ns:AddressDetails>
<ns:Country>
<ns:CountryNameCode>US</ns:CountryNameCode>
<ns:AdministrativeArea>
<ns:AdministrativeAreaName>CA</ns:AdministrativeAreaName>
<ns:SubAdministrativeArea>
<ns:Locality>
<ns:LocalityName>Mountain View</ns:LocalityName>
<ns:Thoroughfare>
<ns:ThoroughfareName>1600 Amphitheatre Pkwy</ns:ThoroughfareName>
</ns:Thoroughfare>
<ns:PostalCode>
<ns:PostalCodeNumber>94043</ns:PostalCodeNumber>
</ns:PostalCode>
</ns:Locality>
</ns:SubAdministrativeArea>
</ns:AdministrativeArea>
</ns:Country>
</ns:AddressDetails>
</Placemark>
</Response>
</kml>
For More Information
XPath Based Mapping - Geocode Example
Map to Element based on an Attribute Value with EclipseLink JAXB (MOXy)
XPath Based Mapping
We have a class, with no control over the source, so no way to annotate it for JAXB. We also have a framework to take care of marshaling. Is there any way for this framework to ask that class whether it is marshallable when no annotations are present?
There is no standard mechanism, but I have seem people accomplish this by trying to create the JAXBContext on the class:
public boolean isValidJAXBClass(Class aClass) {
try {
JAXBContext.newInstance(aClass);
} catch(JAXBException e) {
return false;
}
return true;
}
You don't need any annotations to marshal a JAXB object. You can get around having to
have an #XmlRootElement by wrapping it in a JAXBElement.
If you want an alternative means to represent the metadata, EclipseLink JAXB (MOXy) has a externalized binding file based on the JAXB metadata
A sample file looks something like:
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
<java-types>
<java-type name="org.example.order.PurchaseOrder">
<java-attributes>
<xml-attribute java-attribute="id"/>
<xml-element java-attribute="customer">
<xml-java-type-adapter value="org.example.order.CustomerAdapter"/>
</xml-element>
<xml-element java-attribute="lineItems" name="line-item"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
For more information see:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML
I'm using Java and XStream to parse a google geocode request over http. My idea is to have an Address class with all the geocode attr's (ie. lat/long, city, provice/state etc) but I'm having problems parsing the xml with xstream.
The google response is similar to this:
<?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0"><Response>
<name>98 St. Patrick St, Toronto</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>98 St Patrick St, Toronto, ON, Canada</address>
<AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"> <Country><CountryNameCode>CA</CountryNameCode><CountryName>Canada</CountryName><AdministrativeArea><AdministrativeAreaName>ON</AdministrativeAreaName><Locality><LocalityName>Toronto</LocalityName><Thoroughfare><ThoroughfareName>98 St Patrick St</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>M5T</PostalCodeNumber></PostalCode></Locality></AdministrativeArea></Country></AddressDetails>
<ExtendedData>
<LatLonBox north="43.6560378" south="43.6497426" east="-79.3864912" west="-79.3927864" />
</ExtendedData>
<Point><coordinates>-79.3896388,43.6528902,0</coordinates></Point>
</Placemark>
</Response></kml>
That doesn't show up very well, but the meat of the code is in the AddressDetails tag.
Anyway, I'm new to Java and XStream so the API terminology is a bit confusing for me. I just need to be able to write some mapper that maps all these tags (ie. CountryName) to an attribute within my Address object, (ie. address.country = blah) The address object will be pretty simple, mainly just strings for country name etc and floats for lat/long.
The docs and example just show straight mapping where each xml tag maps directly to the attribute of the same name of the object. In my case however, the tags are named different than the object attr's. A quick point in the right direction is all I'm looking for really.
I've used XStream in several projects. Unfortunately, your problem isn't really what XStream is designed to solve. You might be able to use its converter mechanism to achieve your immediate goal, but you'll run into limitations. In a nutshell, XStream isn't designed to do conversion of Tree Structure A into Tree Structure B -- it's purpose is to convert from a Java domain model into some reasonable XML. XStream is a great tool when you don't care much about the details of the XML produced. If you care more about the XML than the Java objects, look at XMLBeans -- the Java is ugly, but it's incredibly schema-compliant.
For your project, I'd run the Google XML schema through XML beans, generate some Java that will give you a more literate way of hand-coding a converter. You could use a raw DOM tree, but you'd have code like myAddress.setStreet(root.getFirstChild().getAttribute("addr1"))). With XML beans, you say things like myAddress.setStreet(googleResult.getAddress().getStreetName();
I'd ignore JAXB as it's attempt to separate interface from implementation adds needless complexity. Castor might be a good tool to consider as well, but I haven't used it in years.
In a nutshell, there aren't a lot of good Object-to-Object or XML-to-Object converters that handle structure conversion well. Of those I've seen that attempt declarative solutions, all of them seemed much more complicated (and no more maintainable) than using XStream/XmlBeans along with hand-coded structure conversions.
Would it be possible to define a separate class specifically for dealing with XStream's mapping? You could then simply populate your AddressDetails object by querying values out of this other object.
I've ended up just using xpath and populating my own address object manually. Seems to work fine.
Have you tried with json format? It should be the same but you'll need to set a com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver as the driver for XStream
You could use EclipseLink JAXB (MOXy) to do this:
package com.example;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="kml")
public class Address {
private String country;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:CountryName/text()")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
and
#javax.xml.bind.annotation.XmlSchema(
namespace = "http://earth.google.com/kml/2.0",
xmlns = {
#javax.xml.bind.annotation.XmlNs(
prefix = "ns", namespaceURI ="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0")
},
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.example;
A full example is available here:
http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html