Sunday, November 23, 2014

Java Objects to XML and XML to Java Objects - XStream

Java Objects to XML and XML to Java Objects with XStream

 

 import java.io.File; 
 import java.io.FileReader; 
 import java.io.FileWriter; 
 import java.io.IOException; 
 import java.util.Iterator; 
 import java.util.List; 
 import com.thoughtworks.xstream.XStream; 

 public final class XStreamTranslator { 
     private XStream xstream = null; 
     private XStreamTranslator(){ 
         xstream = new XStream(); 
         xstream.ignoreUnknownElements(); 

     /** 
      * Convert a any given Object to a XML String 
      * @param object 
      * @return 
      */ 
     public String toXMLString(Object object) { 
         return xstream.toXML(object);  
     } 
     /** 
      * Convert given XML to an Object 
      * @param xml 
      * @return 
      */ 
     public Object toObject(String xml) { 
         return (Object) xstream.fromXML(xml); 
     } 
     /** 
      * return this class instance 
      * @return 
      */ 
     public static XStreamTranslator getInstance(){ 
         return new XStreamTranslator(); 
     } 
     /** 
      * convert to Object from given File  
      * @param xmlFile 
      * @return 
      * @throws IOException  
      */ 
     public Object toObject(File xmlFile) throws IOException { 
         return xstream.fromXML(new FileReader(xmlFile)); 
     } 
     /** 
      * create XML file from the given object with custom file name 
      * @param fileName  
      * @param file 
      * @throws IOException  
      */ 
     public void toXMLFile(Object objTobeXMLTranslated, String fileName ) throws IOException { 
         FileWriter writer = new FileWriter(fileName); 
         xstream.toXML(objTobeXMLTranslated, writer); 
         writer.close(); 
     } 
     public void toXMLFile(Object objTobeXMLTranslated, String fileName, List omitFieldsRegXList) throws IOException { 
         xstreamInitializeSettings(objTobeXMLTranslated, omitFieldsRegXList); 
         toXMLFile(objTobeXMLTranslated, fileName);     
     }     
     /** 
      * @ 
      * @param objTobeXMLTranslated 
      */ 
     public void xstreamInitializeSettings(Object objTobeXMLTranslated, List omitFieldsRegXList) { 
         if(omitFieldsRegXList != null && omitFieldsRegXList.size() > 0){ 
             Iterator itr = omitFieldsRegXList.iterator(); 
             while(itr.hasNext()){ 
                 String omitEx = itr.next(); 
                 xstream.omitField(objTobeXMLTranslated.getClass(), omitEx); 
             } 
         }  
     } 
     /** 
      * create XML file from the given object, file name is generated automatically (class name) 
      * @param objTobeXMLTranslated 
      * @throws IOException 
      * @throws XStreamTranslateException  
      */ 
     public void toXMLFile(Object objTobeXMLTranslated) throws IOException { 
         toXMLFile(objTobeXMLTranslated,objTobeXMLTranslated.getClass().getName()+".xml"); 
     }

 

  public static Document readFromXml(String input) throws InvalidDataException {
        try {
            return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(IOUtils.toInputStream(input));
        } catch (Exception e) {
            throw new
Exception("Exception occured while converting input String into document", e);
        }
    }  

// Intialize Java object and create XML

 public static String initializeAndWriteToXml(Object objectToWrite) {
        final XStream xstream = new XStream() {
            @Override
            protected MapperWrapper wrapMapper(final MapperWrapper next) {
                return new HibernateMapper(next);
            }
        };
        xstream.registerConverter(new HibernateProxyConverter());
        xstream.registerConverter(new HibernatePersistentCollectionConverter(xstream.getMapper()));
        xstream.registerConverter(new HibernatePersistentMapConverter(xstream.getMapper()));
        xstream.registerConverter(new HibernatePersistentSortedMapConverter(xstream.getMapper()));
        xstream.registerConverter(new HibernatePersistentSortedSetConverter(xstream.getMapper()));

        return xstream.toXML(objectToWrite);
    }

// Return class object

  public static <T> T readFromXml(String xmlContent, Class<T> targetObjectClass) {
        return targetObjectClass.cast(xstream.fromXML(xmlContent));
    }

 }

No comments:

Post a Comment