View Javadoc
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   * Created with IntelliJ IDEA.
13   * User: maheswarang
14   * Date: 4/17/14
15   * Time: 1:10 PM
16   * To change this template use File | Settings | File Templates.
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  }