Here you can find the source of saveToFile(String fileName, T obj)
Parameter | Description |
---|---|
fileName | a parameter |
obj | object |
public static <T> void saveToFile(String fileName, T obj)
//package com.java2s; //License from project: Open Source License import java.io.FileNotFoundException; import java.io.FileOutputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { /**//from www. ja va 2s .c o m * save object to xml file using outputStream, JAXNContext and marshaller * @param fileName * @param obj object */ public static <T> void saveToFile(String fileName, T obj) { FileOutputStream outputStream = null; try { outputStream = new FileOutputStream(fileName); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { // JaxbList<T> jaxbList = new JaxbList<>(list); //File file = openFile(fileName); //String xsdFilename = fileName + ".xsd"; JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass()); // jaxbContext.generateSchema(new XSDSchemaGenerator(xsdFilename)); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, fileName); marshaller.marshal(obj, outputStream); } catch (JAXBException e) { System.out.println("Error saving file " + e.toString()); } } }