Java tutorial
//package com.java2s; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { /** * genXml * * @param o Object Class. * @param path : example c:\path\006DS_AMS20130630.xml */ public static void genXml(Object o, String path) { try { // create JAXB context and initializing Marshaller JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // for getting nice formatted output jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //specify the location and name of xml file to be created File XMLfile = new File(path); // Writing to XML file jaxbMarshaller.marshal(o, XMLfile); // Writing to console jaxbMarshaller.marshal(o, System.out); } catch (JAXBException e) { // some exception occured e.printStackTrace(); } } }