Here you can find the source of marshal(Marshaller marshaller, Object object, String filename)
public static void marshal(Marshaller marshaller, Object object, String filename) throws JAXBException, FileNotFoundException
//package com.java2s; /*/*from ww w .jav a 2 s . co m*/ * Copyright 2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Revision History * Author Date Description * ------------------ -------------- ------------------ * Sang-cheon Park 2012. 8. 24. First Draft. */ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { public static void marshal(Marshaller marshaller, Object object, String filename) throws JAXBException, FileNotFoundException { marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(filename); marshaller.marshal(object, fileOutputStream); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void marshal(Marshaller marshaller, Object object, String filename, String[] schemaLocations) throws JAXBException, FileNotFoundException { marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); FileOutputStream fileOutputStream = null; try { if (schemaLocations != null && schemaLocations.length > 0) { String schemaLocation = new String(); for (int i = 0; i < schemaLocations.length; i++) { if (i > 0) { schemaLocation += " "; } schemaLocation += schemaLocations[i]; } marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation); } fileOutputStream = new FileOutputStream(filename); marshaller.marshal(object, fileOutputStream); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void marshal(Object object, String packageName, String filename, String schemaFile) throws JAXBException, FileNotFoundException { JAXBContext jaxbContext = JAXBContext.newInstance(packageName); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.w3.org/2001/XMLSchema-instance"); marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, schemaFile); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(filename); marshaller.marshal(object, fileOutputStream); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void marshal(Object object, String packageName, String filename) throws JAXBException, FileNotFoundException { JAXBContext jaxbContext = JAXBContext.newInstance(packageName); Marshaller marshaller = jaxbContext.createMarshaller(); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(filename); marshaller.marshal(object, fileOutputStream); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void marshal(Object object, String packageName, String filename, String[] schemaLocations) throws JAXBException, FileNotFoundException { JAXBContext jaxbContext = JAXBContext.newInstance(packageName); Marshaller marshaller = jaxbContext.createMarshaller(); FileOutputStream fileOutputStream = null; try { if (schemaLocations != null && schemaLocations.length > 0) { String schemaLocation = new String(); for (int i = 0; i < schemaLocations.length; i++) { if (i > 0) { schemaLocation += " "; } schemaLocation += schemaLocations[i]; } marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation); } fileOutputStream = new FileOutputStream(filename); marshaller.marshal(object, fileOutputStream); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static String marshal(String packageName, Object object) throws JAXBException, IOException { Marshaller marshaller = null; StringWriter writer = null; try { marshaller = createMarshaller(packageName); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); writer = new StringWriter(); marshaller.marshal(object, writer); return writer.toString(); } finally { if (writer != null) { writer.close(); } } } public static String marshal(String packageName, Object object, String[] schemaLocations) throws JAXBException, IOException { Marshaller marshaller = null; StringWriter writer = null; try { marshaller = createMarshaller(packageName); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); if (schemaLocations != null && schemaLocations.length > 0) { String schemaLocation = new String(); for (int i = 0; i < schemaLocations.length; i++) { if (i > 0) { schemaLocation += " "; } schemaLocation += schemaLocations[i]; } marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation); } writer = new StringWriter(); marshaller.marshal(object, writer); return writer.toString(); } finally { if (writer != null) { writer.close(); } } } public static String marshal(String packageName, Object object, String[] schemaLocations, boolean useFragment) throws JAXBException, IOException { Marshaller marshaller = null; StringWriter writer = null; try { marshaller = createMarshaller(packageName); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); if (useFragment) { marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); } if (schemaLocations != null && schemaLocations.length > 0) { String schemaLocation = new String(); for (int i = 0; i < schemaLocations.length; i++) { if (i > 0) { schemaLocation += " "; } schemaLocation += schemaLocations[i]; } marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation); } writer = new StringWriter(); marshaller.marshal(object, writer); return writer.toString(); } finally { if (writer != null) { writer.close(); } } } public static String marshal(String packageName, Object object, String header) throws JAXBException, IOException { Marshaller marshaller = null; StringWriter writer = null; try { marshaller = createMarshaller(packageName); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false); marshaller.setProperty("com.sun.xml.bind.xmlHeaders", header + "\n"); writer = new StringWriter(); marshaller.marshal(object, writer); return writer.toString(); } finally { if (writer != null) { writer.close(); } } } public static String marshal(Marshaller marshaller, Object object) throws JAXBException, IOException { StringWriter writer = null; try { marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); writer = new StringWriter(); marshaller.marshal(object, writer); return writer.toString(); } finally { if (writer != null) { writer.close(); } } } public static String marshal(Marshaller marshaller, Object object, String[] schemaLocations) throws JAXBException, IOException { StringWriter writer = null; try { marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); if (schemaLocations != null && schemaLocations.length > 0) { String schemaLocation = new String(); for (int i = 0; i < schemaLocations.length; i++) { if (i > 0) { schemaLocation += " "; } schemaLocation += schemaLocations[i]; } marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation); } writer = new StringWriter(); marshaller.marshal(object, writer); return writer.toString(); } finally { if (writer != null) { writer.close(); } } } public static Marshaller createMarshaller(String packageName) throws JAXBException { JAXBContext context = JAXBContext.newInstance(packageName); return context.createMarshaller(); } }