Skip to content

Instantly share code, notes, and snippets.

@friek
Created November 3, 2015 21:27
Show Gist options
  • Select an option

  • Save friek/a4c5647a4ef6b74e7b1c to your computer and use it in GitHub Desktop.

Select an option

Save friek/a4c5647a4ef6b74e7b1c to your computer and use it in GitHub Desktop.
JAXB marshalling and unmarshalling
import javax.xml.bind.JAXB;
import java.io.StringReader;
import java.io.StringWriter;
public class JAXBUtils
{
/**
* Unmarshal an XML string
* @param xml The XML string
* @param type The JAXB class type.
* @return The unmarshalled object.
*/
public <T> T unmarshal(String xml, Class<T> type)
{
StringReader reader = new StringReader(xml);
return javax.xml.bind.JAXB.unmarshal(reader, type);
}
/**
* Marshal an Object to XML.
* @param object The object to marshal.
* @return The XML string representation of the object.
*/
public String marshal(Object object)
{
StringWriter stringWriter = new StringWriter();
JAXB.marshal(object, stringWriter);
return stringWriter.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment