1 package org.kuali.ole;
2
3 import org.kuali.ole.pojo.OLESruItem;
4
5 import javax.xml.bind.JAXBContext;
6 import javax.xml.bind.Marshaller;
7 import javax.xml.bind.Unmarshaller;
8 import java.io.StringReader;
9 import java.io.StringWriter;
10
11
12
13
14
15
16
17
18 public class OLESruItemHandler {
19
20 public String generateXmlFromObject(Object object){
21 StringWriter stringWriter = new StringWriter();
22 try{
23 JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
24 Marshaller marshaller = jaxbContext.createMarshaller();
25 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
26 marshaller.marshal(object,stringWriter);
27 }catch(Exception e){
28 e.printStackTrace();
29 }
30 return stringWriter.toString();
31 }
32
33
34 public Object getObjectFromXml(String xmlContent,Object requiredObject){
35 Object object = null;
36 try{
37 JAXBContext jaxbContext = JAXBContext.newInstance(requiredObject.getClass());
38 Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
39 object = unMarshaller.unmarshal(new StringReader(xmlContent));
40 }catch(Exception e){
41 e.printStackTrace();
42 }
43 return object;
44
45 }
46
47 }