Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Java class from XML file, using XStream

I have many xml files and I would like to use XStream to manage them. Is it possible to generate java classes corresponding to my xml files using XStream?

like image 321
Ulisse Avatar asked Sep 17 '12 06:09

Ulisse


People also ask

How do you create a Java class from an XML schema?

Generate a Java class from an XML Schema using JAXB xsd file or an XML document, which contains the desired Schema. In the main menu, go to Tools | XML Actions | Generate Java Code From XML Schema Using JAXB.

How do you create a class in XML?

The class definition contains a name and possibly a <superClass> element and some <attribute> elements. Contains the name of the superclass. Contains the name of the class or of the attribute being defined. Defines an attribute in the current <class>.

What is Xstreamalias?

Class aliasing is used to create an alias of a fully qualified name of a class in XML. Let us modify our original example and add the following code to it. xstream. alias("student", Student. class); xstream.


1 Answers

XStream is a software to serialize and deserialize a Java Object to and from XML. XStream uses Reflection for this. The class of the objects involved has to exist beforehand.

JAXB is a binding framework, which too does serialization and deserialization. JAXB has annotations to do this work. Bundled with the framework come tools to generate classes (complete with the already mentioned annotations) from an xsd (<-- declaration that describes how a specific xml document is structured).

So you can choose what fits your needs best. Defining classes on your own and use XStream or use JAXB (or other binding frameworks...) and the bundled tools to generate classes. Each variant has advantages and disadvantages... XStream is more flexible but JAXB more strict. JAXB brings additional tools, XStream is more like a tool itself...

like image 104
dertoni Avatar answered Sep 21 '22 03:09

dertoni