JAXB Marshall and Unmarshal from Generated Classes
March 27, 2018
Given classes generated from an XML Schema via a Maven plugin, the following code can be used to serialise to and from XML.
Unmarshal XML into classes
JAXBContext jc = JAXBContext.newInstance(GeneratedClass.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); GeneratedClass javaObject = (GeneratedClass)unmarshaller.unmarshal(new StringReader(xml));
Marshal Java class to XML
JAXBContext jaxbContext = JAXBContext.newInstance(GeneratedClass.class); Marshaller …