What is best practice in converting XML to Java object? - java

I need to convert XML data to Java objects. What would be best practice to convert this XML data to object?
Idea is to fetch data via a web service (it doesn't use WSDL, just HTTP GET queries, so I cannot use any framework) and answers are in XML. What would be best practice to handle this situation?

JAXB is a standard API for doing this: http://java.sun.com/developer/technicalArticles/WebServices/jaxb/

Have a look at XStream. It might not be the quickest, but it is one of the most user friendly and straightforward converters in Java, especially if your model is not complex.

For a JMS project we were marshalling and unmarshalling (going from java to xml and xml to java) XML embedded in TextMessages (string property). We tried JAXB, Jibx, and XMLBeans. We found that XMLBeans worked best for us. Fast, easily configurable, good documentation, and easy Maven integration.

I have used and will continue to use JDOM -> www.jdom.org

Another option is a Sax Parser. It is procedural - i.e. a visitor pattern - but if the xml is fairly lightweight, (and even medium weight) I have found it to be very useful for this.

JAXB API which comes in Java(In built).

I have used JIBX in MQ module. It works very well. Ant config is simple. Used Xsd2Jibx converter to generate the binding files and Java beans from XML schema. Marshalling and un-marshalling allow to specify character-set parameter. It was useful in my project to handle custom character-set. But I found an issue in the binding compiler. If the Java bean has lengthier path name, it generates class file with lengthier file name which will cause issue in Windows XP(it has a maximum file length limit).
I haven't used other APIs. So I am not trying to compare with others. If you decided to use JIBX, I hope this will be helpful.
More details, please refer JIBX website

I've used XStream as well, it is easy to use and customizable. You can add your own custom converters and that was very handy for me...

So surprised more people have not mentioned Jibx. Amazing lib and i think a lot simpler to use than Jaxb. Performance is also fab!

For this you can also consider apache's bitwixt and simple framework for xml

Related

Other replacements for Serialization

I have XML Configuration file and I need to load/save it so What are best ways to Parse XML Files for Load/Save configurations other than Serialization as I don't like the option of Serialization
For a Java application I serialized a pretty complex application state using XStream. For the most part this works extremely good. If the object is really simple this should not be a problem. Another simple alternative I often use to transfer data from Object <-> XML is JAXB with annotations on the Objects, or if the XML structure is the master with an XML Schema from which the classes can be generated.
For C# you could use IXmlSerializable, or DataContract. Multiple answers can be found on SO using both these classes. See this to see how to use DataContract.

Are there any popular libraries to handle writing XSDs in Java without using binding?

I'm currently scraping a bunch of interfaces and building basic XSDs from the information that I derive. I was wondering if anyone knows of a good simple library that I could use to build basic XSDs programatically from the list of simple parameters found by my application. I could do this by simply outputting the variable name and wrapping it in the XSD syntax like this <xs:element name="derivedKey" type="xs:string"> but I'd prefer that some library do this for me in case I decide to do something fancier down the road. Does anyone know of a good library to handle write XSDs without binding them to objects?
Apache XML Schema will definitely solve your problem, i think. You want a Java library so that you create XSD easily and effective way.
Apache XML Scema provides all those features that you need. Apart from that it can also read an XSD, analyse it and modify the same.

File Manipulation libraries

I have a project which I need to manipulate files. things like: create new file by a defined structure(header,data,trail). and then I need to things like search/validate/create/read.
basically I want to map the files to objects and vise versa.(I am willing to map them to objects coz it will be much more comfortable for me to manipulate the fields inside each file via object)
I wonder if any of you deal with such things before? and maybe could recommend me on libraries which could easy my work.
thanks,
ray.
You may want to look at serialization and de-serialization
If you want custom mapping, you need custom coding. I would suggest you look at DataInputStream and DataOutputStream.
Using these you can control the header, records and footer in any binary format you want.
I suggest you generate your serialization (if you need to have the afstest possible speed) or use reflections to do the translation. Just using reflection is pretty fast and much simpler than generating code. ;)
In the end I Found a ORM framework called Canyon which mapping Files to Objects. but still had difficulties. so I have implemented my own ORM file to objects and vise versa.
If you have a defined file layout with different content you should consider to use a template engine like FreeMarker or Velocity to generate your files.
You can define templates here which will be filled with your dynamic content which you have to provide. Definitly better than to use System.out (I mean hard code your template text).
A library which helps for basic file manipulation is Apache Commons IO.
If you realy want to map your files to objects than it would be a Serialization/Deserialization as Angelom mentions. Many libraries help you to do this but the file format is fixed:
JSON: Jackson, GSON
XML: JAXB
If you want the file read by 3rd party as well, how about using some popular existing exchange format such as CSV or XML?
XML is fully supported in standard library. There's plenty of CSV libraries out there, including Apache Commons CSV.

How to create WSDL file given SOAP WSDL operations

I haven't had any experience with web service related development. So, any ideas will be greatly appreciated.
Suppose, I have a file listing draft specification of WSDL operations. Following is one example. How would I go about creating the WSDL file. Is notepad sufficient or do I need to have WSDL editor?
getHostSystemInfo
Returns detailed information about host systems specified via given IDs.
input HostSystemIdCollection(Collection of Strings)
Output HostSystemInfoCollection
HostSystemInfo
Id: mandatory
Properties: Following properties should be provided for host systems
HostSystemName
HostSystemProperty1
HostSystemProperty2
HostSystemProperty3
....
....
If the question is just "how do I create the WSDL" then you could indeed use Notepad and just write it, it's only XML after all. However, writing syntactically correct XML by hand is pretty dull, and error prone. So I would recommend using WSDL aware tooling for example an Eclipse editor
An alternative is to write some Java which expresses the interface, and from it generate the WSDL. There are many ways of doing this, including starting with an EJB and annotating it accordingly. A few googles should help you find what you need.
My experience is that simple POC situations tend to work well starting at the Java. Larger scale projects benfit from considered designs starting at the WSDL.
coding WSDL by hand is a big pain! i used a XML editor for creation of and then generated the stubs with JAXWS. It is important to understand and differences of the WSDL styles, which is not trivial (have a look at WSDL styles). a good help is to import the WSDL schema to your IDE (eclipse, idea) and then work with autocompletion.
just for interest, why are you using WSDL + SOAP. if you have a choice and you use anyway HTTP, have a look at REST. It can make implementation of web-api a LOT easier, both on server side and for api-clients.
If you haven't done any web services before, I would strongly recommend a WSDL Editor. The Netbeans has a plugin that should help.
The other way of doing it, which may be easier is by using the Java annotations defined in JSR 181.
Of course you could use the worst text editor in the world (!) but I'd seriously consider using any decent XML editor or IDE (Eclipse's WSDL support is pretty decent). This will save you a lot of pain and suffer.
Or, if this is an option, you could just annotate a Java class with JAX-WS annotations and have your WSDL dynamically generated from the Java code. Personally, I prefer the WSDL-first approach, the Java-first approach is just a suggestion to get you started.
You could use Axis2 to create that for you.

When and why would you use Apache commons-digester?

Out of all the libraries for inputing and outputting xml with java, in which circumstances is commons-digester the tool of choice?
From the digester wiki
Why use Digester?
Digester is a layer on top of the SAX
xml parser API to make it easier to
process xml input. In particular,
digester makes it easy to create and
initialise a tree of objects based on
an xml input file.
The most common use for Digester is to
process xml-format configuration
files, building a tree of objects
based on that information.
Note that digester can create and
initialise true objects, ie things
that relate to the business goals of
the application and have real
behaviours. Many other tools have a
different goal: to build a model of
the data in the input XML document,
like a W3C DOM does but a little more
friendly.
and
And unlike tools that generate
classes, you can write your
application's classes first, then
later decide to use Digester to build
them from an xml input file. The
result is that your classes are real
classes with real behaviours, that
happen to be initialised from an xml
file, rather than simple "structs"
that just hold data.
As an example of what it's NOT used for:
If, however, you are looking for a direct representation of the input xml document, as
data rather than true objects, then digester is not for you; DOM, jDOM or other more
direct binding tools will be more appropriate.
So, digester will map XML directly into java objects. In some cases that's more useful than having to read through the tree and pull out options.
My first take would be "never"... but perhaps it has its place. I agree with eljenso that it has been surpassed by competition.
So for good efficient and simple object binding/mapping, JAXB is much better, or XStream. Much more convenient and even faster.
EDIT 2019: also, Jackson XML, similar to JAXB in approach but using Jackson annotations
If you want to create and intialize "true" objects from XML, use a decent bean container, like the one provided by Spring.
Also, reading in the XML and processing it yourself using XPath, or using Java/XML binding tools like Castor, are good and maybe more standard alternatives.
I have worked with the Digester when using Struts, but it seems that it has been surpassed by other tools and frameworks for the possible uses it has.

Categories