aspose file tools
The moose likes Other JSE/JEE APIs and the fly likes how to get XML from javaobject using JAXB Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Reply Bookmark "how to get XML from javaobject using JAXB" Watch "how to get XML from javaobject using JAXB" New topic
Author

how to get XML from javaobject using JAXB

krishna chitturi
Greenhorn

Joined: Nov 26, 2002
Posts: 11
hi
i have java object and i need to get xml from it, we are using JAXB for unmarshal(java object to xml).we are generating JAXB object it has two methods named marshal and unmarshal.unmarshal is used to get java object from XML , how to call marshal to get xml from jaxb.that unmarshal method is taking Marshallaer as parameter, how can i pass Marshaller as Marshaller is private final class.
please help me out.
PMP Kannan
Greenhorn

Joined: Jan 23, 2003
Posts: 10
hello Krishna,
first u should have a DTD for the XML document
let's assume that u have a xml file named Item.xml which has some elements and a DTD file named Item.dtd.
then u should ceate the XML binding schema for the xml doc.
ex:
<xml-java-binding-schema version="1.0ea">
<element name="item" type="class" root="true" />
</xml-java-binding-schema>
save this as filename.xjs
in the above file the element name is item which is the root element.
then generate java files using the schema compiler.
before all this u should set the classpath pointing to ur jaxb jar file.
now compile to generate java files.
suppose the DTD name is Item.dtd and schema name is Item.xjs, then the compilation will be
java com.sun.tools.xjc.Main "Item.dtd" "Item.xjs"
after this java files are generated.
u will get a java file named Item.java which has methods to manipulate the xml data
u can Unmarshall it like this(from XML to JAVA)

Item item = new Item();
File sItem = null;
try {
sItem= new File("Item.xml");
fileInputStream = new FileInputStream(sItem);
item = item.unmarshal(fileInputStream);
catch(Exception e){}
// do the next step to see the xml file printed in the console
System.out.println(item.toString());// will print the xml Doc as a string

u can marshall it back like this(from JAVA to XML)

Item item = new Item();
FileOutputStream fileOutputStream = null;
try{
File changedItem = new File("NewItem.xml");
fileOutputStream = new FileOutputStream(changedItem);
item.marshal(fileOutputStream);
} catch (IOException e){
e.printStackTrace();
} finally {
fileOutputStream.close();

check the file written in the directory. it will be a xml file
that's it
cheer's
kannan
In the country of the blind, one eye'd man is the king
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how to get XML from javaobject using JAXB
 
Similar Threads
How to marshal data from a SOAP header and write it to disc
how to get XML form javaobject using JAXB
What is the best practice to store an object graph?
how to get xml from java object using JAXB
how to get XML from javaobject using JAXB