I am using XStream for converting the java object to xml. But i am not getting the proper output. When i am iterating the object, I am able to get the information. But when that object is converted to xml by XStream, the data changes. I dont know what will be the problem.
Can you provide some alternate with some samples.
Thanks in advance.
Easwar
Castor Mapping. can be a good alternative.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
Have you considered using a JAXB implementation (Metro, MOXy, Apache JaxMe, etc)? Here is an example of mapping the same object model with JAXB and XStream for comparison:
http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-xstream.html
In addition since JAXB is part of Java EE, it is the default binding layer for other Java EE technologies such as JAX-RS and JAX-WS:
http://blog.bdoughan.com/2010/08/creating-restful-web-service-part-35.html
http://blog.bdoughan.com/2011/04/moxys-xml-metadata-in-jax-rs-service.html
MOXy also contains some very useful extensions beyond the JAXB specification:
http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html
http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html
http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
http://blog.bdoughan.com/2011/06/moxy-extensible-models-multi-tenant.html
Related
Is it possible using Wildfly's JAXB implementation?
There is an answer Converting Java Object to Json using Marshaller, but it seems to be implementation specific.
if you were constrained to use the jaxb (for example, when you need the flexibility to have both xml and json class in the same spot) you might want to refer to these
Wildfly Reference
A Simple tutorial, from MKYong
but if the jaxb is not a constraint to have, i reccomend using either GSON or jackson. they are both relatively easy to use when reading json to a POJO and vice-versa
JSR-353 does not include object binding. If you want to marshal/unmarshal JSON you'd have to using something like Jackson.
It does you know real good now, but there is JSR-367 to include bindings in Java EE 8.
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.
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/
I need an open-source tool which can convert POJOs to XML and JSON strings.
Jersey (and probably other JAX-RS implementations) would fit these requirements if mappings could be configured through xml-files but not through annotations.
Is there anything suitable?
POJO to XML
JAXB is the Java standard (JSR-222) for converting Java objects to/from XML (I am a member of the JAXB expert group):
http://bdoughan.blogspot.com/2010/07/jaxb-xml-binding-standard.html
POJO to JSON
People have been using JAXB with Jettison to produce JSON. This is how Jersey (JAX-RS reference implementation) converts POJOs to JSON by default.
http://bdoughan.blogspot.com/2011/04/jaxb-and-json-via-jettison.html
XML Configuration
If you require the ability to configure the metadata via XML, then you can use EclipseLink JAXB (MOXy), I'm the MOXy tech lead:
http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html
There are lots of suitable JSON libraries; aside from GSON that was mentioned, Jackson is an obvious choice. Typically you don't need any annotations; but even if you neded, Jackson can use so-called mix-in annotations which means associating configuration, not modifying actual POJOs. Finally, Jackson supports JAXB annotations if you must use them (for example, when using JAXB for producing XML).
And as an added bonus, there is Jackson extension module, jackson-xml-databind, which can data bind POJOs to and from XML, so you can just use one library for both.
Have you tried Castor?
http://www.castor.org/
Supports XML mapping -
http://www.castor.org/xml-mapping.html
From XML, you could use json-lib if you need JSON.
http://answers.oreilly.com/topic/278-how-to-convert-xml-to-json-in-java/
Check out GSON for converting POJOs to JSON:
http://code.google.com/p/google-gson/
We've been using for a couple of years now. Very excellent library.
For POJOs to XML, Jersey uses JAXB or you could roll your own serializer.
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