/** * @author Hyacinthe MENIET * Created on 15 juil. 07 */ package net.dotmyself.j2xml; import java.io.FileWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; /** * Marshalles the created java Objects to the specified XML Document. */ public class Java2XML { /** * The main method. * @param args the path to file to generate. * @throws Exception */ public static void main(String[] args) throws Exception { if(args == null || args.length < 1) { throw new IllegalArgumentException("You must indicate a path to file to generate"); } String fileName = args[0]; // Instanciates an ObjectFactory ObjectFactory objFactory = new ObjectFactory(); // Creates first rental Rental rental1 = objFactory.createRental(); rental1.setBegindate("15/07/2007"); rental1.setTitle("Casino Royal"); rental1.setType("DVD"); rental1.setUser(1); // Creates second rental Rental rental2 = objFactory.createRental(); rental2.setBegindate("16/07/2007"); rental2.setTitle("Borat"); rental2.setType("VHS"); rental2.setUser(3); // Stores rentals Rentals rentals = objFactory.createRentals(); rentals.getRental().add(rental1); rentals.getRental().add(rental2); // Marshalles objects to the specified file JAXBContext context = JAXBContext.newInstance(Rentals.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(rentals, new FileWriter(fileName)); } }