Created
November 3, 2015 21:27
-
-
Save friek/a4c5647a4ef6b74e7b1c to your computer and use it in GitHub Desktop.
JAXB marshalling and unmarshalling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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