Jaxb tutorial part-1----unmarshalling the xml
Step1:-> You have to download the plugin for the eclipse and the paste it in the plugin folder of the xml
Step2:->Download any sitemesh.xsd or any xsd right click on it and generate the classes it will generate object factory class, package info and your main jaxb class with name of your xsd
Step3:->Now unmarshall it using following code
package com.jaxbtest.classes;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class MyJaxbTest {
private static JAXBContext jaxbContext = null;
private static Unmarshaller unmarshaller = null;
public static void main (String[] args)
{
try {
//JAXBContext.newInstance(“<package-name>”)
//”<package-name>” refers to the package in which the auto-genrated class files due to xjc command are available.
jaxbContext=JAXBContext.newInstance(Class.forName("com.jaxbtest.classes.Show"));//pass your class here
unmarshaller=jaxbContext.createUnmarshaller();
File file = new File("D://order.xml");
if (!file.exists()) {
System.out.println("XML file not found");
System.exit(0);
}
Show cfg=(Show)unmarshaller.unmarshal(file); //you can cast the jaxb object to your class object and get required data
} catch (JAXBException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Step4:-If you have namespace in your xml it will generate a package-info.java class which will have uri information if you dont want you can delete it and add namespace at the top of the class
Step1:-> You have to download the plugin for the eclipse and the paste it in the plugin folder of the xml
Step2:->Download any sitemesh.xsd or any xsd right click on it and generate the classes it will generate object factory class, package info and your main jaxb class with name of your xsd
Step3:->Now unmarshall it using following code
package com.jaxbtest.classes;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class MyJaxbTest {
private static JAXBContext jaxbContext = null;
private static Unmarshaller unmarshaller = null;
public static void main (String[] args)
{
try {
//JAXBContext.newInstance(“<package-name>”)
//”<package-name>” refers to the package in which the auto-genrated class files due to xjc command are available.
jaxbContext=JAXBContext.newInstance(Class.forName("com.jaxbtest.classes.Show"));//pass your class here
unmarshaller=jaxbContext.createUnmarshaller();
File file = new File("D://order.xml");
if (!file.exists()) {
System.out.println("XML file not found");
System.exit(0);
}
Show cfg=(Show)unmarshaller.unmarshal(file); //you can cast the jaxb object to your class object and get required data
} catch (JAXBException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Step4:-If you have namespace in your xml it will generate a package-info.java class which will have uri information if you dont want you can delete it and add namespace at the top of the class