Here you can find the source of marshalV2(Class
public static <T> String marshalV2(Class<T> clazz, T obj, String uri, String nodeName) throws JAXBException
//package com.java2s; //License from project: Open Source License import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.namespace.QName; public class Main { public static <T> String marshalV2(Class<T> clazz, T obj, String uri, String nodeName) throws JAXBException { StringWriter out = new StringWriter(); JAXBContext context = JAXBContext.newInstance(clazz); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(new JAXBElement<T>(new QName(uri, nodeName), clazz, obj), out); return out.toString(); }//from ww w. j a v a2 s .c o m public static <T> String marshal(Class<T> clazz, T obj) throws JAXBException { StringWriter out = new StringWriter(); JAXBContext context = JAXBContext.newInstance(clazz); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(obj, out); return out.toString(); } }