How can i convert java object to xml? I am trying to convert an incoming java object to xml in spring integration with a converter bean. is there another way than marshalling in Jaxb ? like using #TypeConverter. Or implementing converter class.
I'm not familiar with #TypeConverter, but looks that that is an EclipseLink feature for JPA. In the end it has this JavaDoc on the #Convert:
/**
* Constant name for the reserved XML converter.
* This will use JAXB to convert the object to and from XML.
*/
public static final String XML = "xml";
So, it still would require from you a JaxB Marshaller to be configured.
Not sure from here why do you ask about Spring Integration if your end goal is JPA...
Anyway: the best way to convert object to XML in Spring Integration is indeed use a MarshallingTransformer which could be configured with any org.springframework.oxm.Marshaller impl, not only JaxB.
If you are looking for some conversion way in between, you may also look into MappingJackson2MessageConverter when you inject an XmlMapper from Jackson lib.
For better assisting you, we definitely need to know more about your use-case and what and how you'd like to marshal into an XML.
I am looking to automate validating JSON instances against XML instances. What would be a good way to achieve this? I was thinking of mapping fields but then again this would just solve the problem say for one instance and the script would need to be updated for each instance.
Are there any libraries in Java that can aid in this?
No, use XML Schema (XSD) for validating XML; use JSON Schema for validating JSON.
If you're looking to parse from XML to JSON (unmarshal) or serialize JSON to XML (marshal), see JSONIX.
You may be using validate in an unconventional sense which would be satisfied by parsing from XML to JSON using JSONIX for comparison with other JSON.
I found that many of the tutorials on the net and here on SO also refer to net.sf.json library to convert an xml file to json object.
But, I want an alternative preferably using Gson. Is it possible? I don't have well defined Java Classes for the XML file. But, I just want to convert the xml file to com.google.gson.JsonObject. How to achieve it?
I've done the same using JAXB to convert my xml to an object, and passing the object to gson.
I know it takes one additional step, but that worked convenient for me.
Upon converting xml to jaxb see also:
Use JAXB to create Object from XML String
I have a simple xml file and would like to have different complex type call a factory w/ the parameters rather than creating a concrete class directly.
I would rather use something like XMLBeans w/ factory support rather than writing my own DOM or SAX code. From what I can tell Apache Digester iteself is also not a solution...
The following articles may help you do what you want with JAXB:
Instantiate object with parameters:
http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html
Leveraging the ObjectFactory:
http://bdoughan.blogspot.com/2010/07/moxy-jaxb-map-interfaces-to-xml.html
If you have a Java object and an XML schema (XSD), what is the best way to take that object and convert it into an xml file in line with the schema. The object and the schema do not know about each other (in that the java classes weren't created from the schema).
For example, in the class, there may be an integer field 'totalCountValue' which would correspond to an element called 'countTotal' in the xsd file. Is there a way of creating a mapping that will say "if the object contains an int totalCountValue, create an element called 'countTotal' and put it into the XML".
Similarly, there may be a field in the object that should be ignored, or a list in the object that should correspond to multiple XML elements.
I looked at XStream, but didn't see any (obvious) way of doing it. Are there other XML libraries that can simplify this task?
I believe this can be achieved via JAXB using it's annotations. I've usually found it much easier to generate Objects from JAXB ( as defined in your schema) using XJC than to map an existing Java object to match my schema. YMMV.
I'm doing Object do XML serialization with XStream. What don't you find "obvious" with this serializer? Once you get the hang of it its very simple.
In the example you provided you could have something like this:
...
XStream xstream = new XStream(new DomDriver());
xstream.alias("myclass", MyClass.class);
xstream.aliasField("countTotal", MyClass.class, "totalCountValue");
String xml = xstream.toXML(this);
...
for this sample class:
class MyClass {
private int totalCountValue;
public MyClass() {
}
}
If you find some serializer more simple or "cool" than this please share it with us. I'm also looking for a change...
Check the XStream mini tutorial here
I use a java library called JiBx to do this work. You need to write a mapping file (in XML) to describe how you want the XML Schema elements to map to the java objects. There are a couple of generator tools to help with automating the process. Plus it's really fast.
I tried out most of the libraries suggested in order to see which one is most suited to my needs. I also tried out a library that was not mentioned here, but suggested by a colleague, which was a StAX implementation called Woodstox.
Admittedly my testing was not complete for all of these libraries, but for the purpose mentioned in the question, I found Woodstox to be the best. It is fastest for marshalling (in my testing, beating XStream by around 30~40%). It is also fairly easy to use and control.
The drawback to this approach is that the XML created (since it is defined by me) needs to be run through a validator to ensure that it is correct with the schema.
You can use a library from Apache Commons called Betwixt. It can map a bean to XML and then back again if you need to round trip.
Take a look at JDOM.
I would say JAXB or Castor. I have found Castor to be easier to use and more reliable, but JAXB is the standard