Automatic class model creation based on xml in java - java

I have an xml file format and using that xml I want to create skeleton classes needed to serialize and de-serialize that xml. I am using Java and XStream for this.
There is tool in .net world which creates classes using xml. Is there anything similar in Java world?

I have not used XStream myself, so this solution may not 100% work for you. However, the simplest approach in Java is to use Java's Architectural Binding for XML (JAXB) API and tools. JAXB was included as part of the JDK with the release of Java 6. To generate Java code from an XML schema you would use the xjc command that comes with the JDK. Here is an example:
> xjc schemas\my-schema.xsd -d src\java -p com.company.model
This code generation method will create Java Objects that include JAXB-specific annotations that are used by the Marshaller to map the Java object to its XML format and vice versa. It will also contain number of warnings stating that the code was auto-generated and should not be modified. As long as you are not trying to automatically keep the code in synch with your XML you could ignore these messages.
Now, as I mentioned, this technique does generate JAXB annotated classes, however, the generated code may still be compatible with XStream as I believe XStream uses simple attribute name -> xml node name conversion logic.

Related

how to convert string in the form of xml to xml format in java

I have xml data in the form of string. I would like to know how to convert it using Java.
In MULE ESB I have the string message in logger showing it as:
[payload:java.lang.String]
I need to store each elements and give a response in XML.
How do I configure it with JAXB or any other process?
<EAI_MESSAGE>
<EAI_HEADER>
<MsgFormat>ISO8583</MsgFormat>
<MsgVersion>1.0.1.0</MsgVersion>
</EAI_HEADER>
<EAI_BODY>
<FundTransferRequest>
<CustomerId>CT1010</CustomerId>
<SourceAccNo>12346789012345</SourceAccNo>
<SourceAccName>SOURAV MUKHERJEE</SourceAccName>
<SourceAccType>SAVINGS</SourceAccType>
<SourceIFSC>SBI1010</SourceIFSC>
<Amount>5000.00</Amount>
</FundTransferRequest>`enter code here`
</EAI_BODY>
Your are looking in the correct direction when you mention JAXB. JDK comes with xjc which allows you to auto-generate classes that are needed to unmarshall and marshall XML files. You can follow the following steps to build a solutions
Build XML Schema for input and output XML format. There are number of tools available to achieve this. (How to generate JAXB classes from just XML)
Use the XML schemas to generate JAXB java library. (Generate your JAXB classes in a second with xjc)
Now configure you java application to unmarshall (build java objects from XML) using input JAXB library. (Using JAXB to Generate Java Objects from XML Document)
Build Java object structure using classes generated using XJC for needed for output XML format using the Java objects created by unmarshalling. This is pure Java programming task
Now, generate the output XML needed using marshaller in JAXB library
This seems complex but except for generating output Java data structure from input Java, rest of the process is straight forward. Hope this helps.

Generate JSON binding and parser from schema without reflection and annotations

In Java SE/EE, you can generate a Java model from an XML schema and then marshal and unmarshal Java to XML or JSON. The marshallers use reflection and annotations which are not available for most Java mobile APIs which have more or less a Java 1.4 compatibility level.
Is there any existing solution for Java-to-JSON binding on such restricted platforms that does not use platform specific APIs (e.g. Android)?
A possible approach might be an XJC plugin that suppresses annotations and adds marshal() and unmarshal() methods to each model class.
See also: Is there a need in JAXB implementation for Android?
I have not seen such solutions.
It is surely possible to workaround both annotations and reflection by generating the marshalling/unmarshalling code directly in schema-derived classes - just as you suggest. Actually JAXB 1 worked in this way in many senses. However I'm not sure if there's a pressing need for such technology.
ps. I've implemented a JAXB analog for JavaScript (compiler is based on XJC) so it would be definitely possible.
There is a solution named JSONx Framework, but it requires jdk1.8 at the least.

Generate XSD files in Java

Is there any way (not from classes/JAXB) to create XSD schemas in Java? I can parse it with the help of some libraries ie XSOM, Jdom etc. But could not find anything to create.
I don't know about any easy to use way.
I considered using dom4j (because I use it for other purposes, but any other generic xml manipulation library is equivalent) and manualy create it (1).
But then I realized I can use JAXB to generate object model of xml schema, populate it with what i wanted (turned out to be much less comfortable than I hoped for) and marshall it(2). Its via JAXB, but without creating classes for your schema, so maybe it can be usefull to you.
via 2 is hard (but not impossible) to create invalid schema, but its sometimes hard to find out how to create schema I wanted. So I ended creating it in editor, then unmarshalling it and exploring its object representation. In the end, creating it via 1) and then validating it (which I had to do anyway) would be less chore.
Take a look at apache xerces
http://xerces.apache.org/xerces2-j/xml-schema.html
still there's no out of the box solution to handle xsd files
You could use the Eclipse XSD project, part of the Eclipse Model Development Tools. It provides a data model and API for programatically creating schemas.
To create XSD is to use java classes/coding (is inevitable):
Just use Java DOM and create a document then the main node and create other nodes to attach to it and voila!

Parsing XML at RunTime based on XSD with JAXB 2.x

Have a requirement so want to check its feasibility.We are using JAXB 2.x for unmarshling XML in to out existing POJO classes.below is the process we are following..
Based on the XSD provided we have already created required classes using JAXB utility.
On Run time we are passing only XML file and we first validating the XML with the preexisting XSD and if success than will move ahead with the Unmarshling.
For XML creation same process but in reverse order.
Now there is one requirement so the client want that they can pass XML and XSD to the method being developed and this method should hand over the common object as return by parsing that XML and later on they will handle the process of mapping that generic Object with the specific classes.
My question is,Is it possible in Jaxb 2.x that based on the XSD supplied it will first create required classes on the fly and than will parse XML and hand over the result to the client module so they can do the mapping work or is there any way to achieve that functionality??
Thanks in advance
In EclipseLink MOXy (I'm the tech lead) we have a feature called Dynamic JAXB, that lets you bootstrap from an XML schema and use generic objects. You might find this approach useful:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic
Dynamic java bean from xsd
I know it Is doable. One student in my lab did almost the same thing on a different subject. Though, you will have to play with classloader to be able to load dynamically the classes you've jest created. Once that is done, you can parse the XML using the classloader that includes the new classes.
Of course, it is not as easy as it sounds like...
Have a look at the sample samples/inline-customize
located at http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/2.0/jaxb/samples.html

In Java for XML marshalling/unmarshalling which utility should we use from JAXB, JIBX, CASTOR, ADB etx?

In Java for XML marshalling/unmarshalling one can use JAXB, JIBX, CASTOR, ADB etc.
But out of these which is most generic & commonly used? Or is there any other utility available?
The standard is the JAXB (JSR 222), and the famous project with this name is the reference implementation. Unlike JAXB 1.0, JAXB 2.0 RI is quite good, and I've used it a lot. Other libraries implements the JAXB standard (I think tha Castor and JiBX, but I have no experience with them).
I've also use XStream, which was very easy and simple - it has a proprietary API though.
I don't know of any benchmark other than https://bindmark.dev.java.net/old-index.html - notice it is a 4 year old one. perhaps you can take it's ideas or any usable code it may have and run some tests yourself.
I personally use XMLBeans. I like it more than JAXB (and I am biased in favour of things from Sun since they should be more "standard"). I cannot speak to the commonly used part of it though.
JAXB is preferred because of following reasons:
No extra jaxb libraries are required if you are using JDK1.6 or above, because JAXB is bundled in JDK 1.6.
The jaxbMarshaller.unmarshal() contains a lot of overloaded methods, find one that suit yours.
JAXB is pretty easy to understand and operate. what it requires is couple of annotations like #XmlRootElement and #XmlAccessorType along with four lines of code
Modifying XML files in Java is much easier using JAXB as compared to other XML parsers like DOM and SAX because you only deal with POJOs
JAXB is more memory efficient than DOM or SAX parser.
JAXB doesn't require XML schema to work. Though you can use XML Schema for generating corresponding Java classes and its pretty useful if you have large and complex XML Schema which will result in huge number of classes but for simple usage you can just annotate your object with relevant XML annotations provided by JAXB package i.e. java.xml.binding and you are good to go.
I use CASTOR mapping and IMHO it is very easy to use provided you are familiar with XML. All you need to do is to create Java objects and map them to the XMl using a mapping file. The mapping file is required if your XML is a complex one. If it is a simple and straight forward XML, CASTOR will itself look for respective Java class which matches the XML element name [Castor uses Java's Reflection APIs to do this].

Categories

Resources