Declarative XML -> POJO conversion - java

I have to write a process (in Java) which periodically hits a URL, reads the returned XML document, and persists that data into the DB. This data is further used by my application, so I have modeled them as Hibernate-mapped POJOs.
I can parse the XML and then create appropriate POJOs, but I was looking for a simpler declarative approach. What libraries are available which can take a input configuration and create the POJOs from the XML document?

Another alternative could be JiBX
Also, although you said you don't want to parse the XML, XPath can be a very concise way of extracting the content you are interested in?

JAXB can automatically create classes based on an XML Schema (assuming you have one for the XML source). At runtime, it can then convert the XML document into POJOs representing the XML. It is declarative in that you can tweak the Schema-to-class mapping, a little.
If I understand your task correctly, this is pretty much the use-case JAXB was designed for (though it can do other things too). It's part of Java 1.6 (maybe 1.5 too?), in packages: javax.xml.bind.*

You can use XStream to deserialize the XML and map it directly to the Hibernate-mapped POJOs.
Cheers.

Using Hibernate you can directly map XML to table. This is experimental feature. Check here
http://www.hibernate.org/hib_docs/v3/reference/en-US/html/xml.html

EclipseLink JAXB (MOXy) has extensions for mapping JPA entities to XML (JPA entities have things like embedded ID classes, lazy loading, and compound key relationships that need special handling), I'm not aware of any other OXM solution that does this.
For more information see:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA

Related

Handling frequent schema change in the jaxb

In my application we are using JAXB for unmarshalling incoming xml response.
JAXB pojos are further mapped to DAOs for DB operations.
My issue is our service provider is going to frequently update their schema(xsd)(insertion, deletion of elements and attributes) and i dont want to keep generating POJOs for different versions of the same schema and updating my other codes.
Please suggest a solution to handle this scenario.
You may want to look into MOXy's Dynamic JAXB feature. It allows you to bootstrap a JAXBContext directly from the schema, and interact with your model objects in a dynamic, schema-centric way. Using this approach your application code would need to be updated to work with any new schema elements, but your JAXB binding code would stay the same.
Please see: http://www.eclipse.org/eclipselink/documentation/2.4/moxy/dynamic_jaxb.htm#sthref191
Hope this helps,
Rick

Convert XML to database with Java

Need: take in XML and save data to database.
Currently using: JAXB to convert the XML Schema to java classes. Then I intend to use JPA to persist the objects marshalled by JAXB.
Problem: I want something to bridge the gap. After JAXB generates the Java classes I have to manually annotate all java.util.Date fields with #Temporal; I have to put #Entity on the top of every generated class...etc.
I came across Hyperjaxb. But I can find little documentation on it, and can't get it to work.
I am open to completely different approaches. This seems like it would be a common problem, so maybe there is a generic solution.
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.
If you already have an existing database schema, the you could use the Dali tool in Eclipse (part of the Web Tools Project) to generate your JPA entities form the database:
http://www.eclipse.org/webtools/dali/
JAXB is configuration by exception, this means you only need to add annotations where you want to override the default behaviour. Dali also has tooling to make adding JAXB annotations easier:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/TheBasics
JPA entities sometimes use bidirectional relationships and composite keys, these can be tricky to map to XML. EclipseLink JAXB (MOXy) contains some extensions to make this easier (note EclipseLink also offers a JPA implementation):
http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA
Hyperjaxb does exactly what you're trying to achieve. Here's the documentation:
http://confluence.highsource.org/display/HJ3/Home
Here's a tutorial to get you started:
http://confluence.highsource.org/display/HJ3/Purchase+Order+Tutorial
I can suggest two options:
Option 1. Define your entity types separately, having the relevant JPA annotations, generate your JAXB types from the schema, and at runtime, map one to the other. If it is a simple mapping you can use Apache BeanUtils to just copy over the attributes from one bean to the other, if it is a more complex mapping then you can use a framework like dozer
Option 2: Start from entity types, generate the schema from your entity types or manually keep the entity types and the schema in synch. More like the option that you have described, except that the authoritative source is the java code than the schema.

Jackson processor for java bean to json\xml serialization

Hi I am having a java bean and i need to serialize it to json. For this purpose i am using the jackson processor which is very powerful. But I also need to convert the java bean to XML.
Can this be achieved using the jackson processor ?? If yes then please provide with the links where I can get the examples.
Thanks!!!!
If you want to keep Jackson and JSON out of your pojos, you can create a translation layer that can translate to a JAXB object and use pure JAXB (JAXB being one possible implementation in this case). If your domain objects map straight to the rendered JSON or you can use mixins/PropertyNamingStrategy, you will need no annotations in your pojos. I'm not sure if this is applicable in your situation but I know that many environments strive for this.
Definitely! Jackson obviously has first-class support for JSON; but there is also simple extension module to do "mini-JAXB": jackson-xml-databind.
With Jackson's support for JAXB annotations (or not, if you just prefer Jackson's own annotations & xml module's couple additional ones), it's definitely possible to do both JSON and XML just using Jackson functionality.
I mostly recommend this for cases where XML support is a legacy thing (which is what most new services do). If XML is the main focus, it may make more sense to use JAXB.
But even then I recommend against using conversion layers from XML to JSON; ones I have seen used have been plagues with issues when they conversion at data format layer, which IMO is completely wrong place to do it. This is also why Jackson does not try converting JSON to XML (or vice versa); rather, it only supports converting POJOs to/from external data formats.
Yes it is possible. You would need to annotate your Java bean using the JAXB annotations which will give you the XML marshalling and unmarshalling support. Then, using Jackson's support for JAXB annotations you can go back and forth between Java and JSON.

Is it possible to auto generate annotated-mapped POJO classes using techniques like Hibernate, JIBX etc?

Hibernate can auto-generate schemas from properly annotated POJO classes. And I also know that JIBX can create a data model (set of classes) out of properly structured XML schemas. Is there a way to automaticaly generate annotated-mapped classes from an XML schema? Or is it just possible to run a tool on a set of POJO classes, and expect it to create meaningful annotations on the specified classes? So later on we can create database schemas using these classes. To annotate every class that JIBX produces takes actually more work than manually designing the database schema according to the xml schema.
The Hyperjaxb project will generate JAXB classes from an XML schema that contain JPA annotations that could be used to create a database schema.
http://java.net/projects/hyperjaxb
Nice question! We had the same problem and we ended up developing the POJO generator with Freemarker.
By the way, the requirements to these POJOs may strongly vary, so, if such tool exists, it must have quite bloated configuration.

gdata to java object mapping

hi
I started learning google data api.
I have one question
What will be the best way of parsing xml google data xml ?
1 > Should I go with manually parsing xml ?
Or
2 > XML to java object mapping technique ?
I am thinking about going with 2 way, As it will require writing less code from my side.
But I don't know how slow it will be in comparison to 1 way.
How slow is xml to java mapping technique ?
Is there any other better way to parse gdata xml ?
How slow is xml to java mapping
technique ?
The answer to this question depends on what you are comparing the xml to Java mapping technique to. Many object-to-XML solutions have been around for quite a while and have learned a number of tricks to do this conversion.
Is there any other better way to parse
gdata xml ?
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB (JSR-222) expert group.
I'm not that familiar with the Google Data API, but after a quick google, it appears to be related to Atom which can easily be handled by any of the JAXB implementations:
Metro (reference implementation included in Java SE 6)
EclipseLink JAXB (MOXy)
Apache JaxMe
For an Atom example see:
http://bdoughan.blogspot.com/2010/09/processing-atom-feeds-with-jaxb.html
With JAXB you can also start with your own object model and add annotations to apply the XML mapping:
http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html
http://bdoughan.blogspot.com/2010/10/jaxb-and-xsd-choice-xmlelements.html
http://bdoughan.blogspot.com/2010/09/jaxb-collection-properties.html
MOXy also contains an XPath mapping extension that makes mapping your object model even easier:
http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
Take a look at Caster project. Judging from your question, i don't think you would want to go though the trouble to parse the XML using the correct schema and create POJO's to match them (which is troublesome anyway).
I used Caster sometime ago, it is not the best, but got my problem solved pretty fast.
http://www.castor.org/

Categories

Resources