Here you can find the source of marshallJAXBObject(String namespace, Object o)
public static String marshallJAXBObject(String namespace, Object o) throws JAXBException
//package com.java2s; /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { private static Map<String, JAXBContext> JAXBContextInstances = new HashMap<String, JAXBContext>(); /**// www .ja v a2 s . c o m * Marshall a JAXB object and return the XML as a string. The XML declaration will be added. */ public static String marshallJAXBObject(String namespace, Object o) throws JAXBException { return marshallJAXBObject(namespace, o, true); } /** * Marshall a JAXB object and return the XML as a string */ public static String marshallJAXBObject(String namespace, Object o, boolean addXMLDeclaration) throws JAXBException { JAXBContext jc = getJAXBContext(namespace); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", addXMLDeclaration); StringWriter sw = new StringWriter(); marshaller.marshal(o, sw); return sw.toString(); } public static JAXBContext getJAXBContext(String namespace) throws JAXBException { if (!JAXBContextInstances.containsKey(namespace)) JAXBContextInstances.put(namespace, JAXBContext.newInstance(namespace)); return JAXBContextInstances.get(namespace); } }