Here you can find the source of marshallUnmarshall(T inObj)
Parameter | Description |
---|---|
inObj | a parameter |
Parameter | Description |
---|---|
Exception | an exception |
public static <T> T marshallUnmarshall(T inObj) throws Exception
//package com.java2s; /*//from ww w. jav a2 s . c o m * Copyright (c) 2007-2012 The Broad Institute, Inc. * SOFTWARE COPYRIGHT NOTICE * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved. * * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality. * * This software is licensed under the terms of the GNU Lesser General Public License (LGPL), * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php. */ import org.w3c.dom.Document; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; public class Main { /** * Marshalls {@code inObj} and unmarshalls the result, returning the * unmarshalled version * * @param inObj * @return * @throws Exception */ public static <T> T marshallUnmarshall(T inObj) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); JAXBContext jc = JAXBContext.newInstance(inObj.getClass()); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FRAGMENT, true); //This JAXBElement business is necessary because we don't know if we have @XmlRootElement on inObj JAXBElement inel = new JAXBElement(new QName("", "obj"), inObj.getClass(), inObj); //m.marshal(inel, System.out); m.marshal(inel, doc); Unmarshaller u = jc.createUnmarshaller(); JAXBElement el = (JAXBElement) u.unmarshal(doc, inObj.getClass()); return (T) el.getValue(); } }