Monday, August 27, 2012

Java Generics and XML

In my world of .Net, I know exactly how to serialize and deserialize between Objects and an XML file.  I typically carry around functions with Generics so I can minimize my code.

I had to do something in Java, and struggled to find the exact same thing out on the web.  So I made my own set of functions using JAXB.

**************************************************

import java.io.*;

import javax.xml.*;
import javax.xml.bind.*;
import javax.xml.validation.*;

import org.xml.sax.SAXException;

/**
 * Generic Utilitarian Class 
 *
 */
public class GenericUtilities
{
/**
* Class for taking an XML file and converting it to define objects
* @param xmlFile - A valid XML file for pulling object definitions from
* @param schemaFile - 
* @param type - The class of the object to convert the XML into
* @return - The instance of the defined class, constructed from the XML
* @throws JAXBException 
* @throws FileNotFoundException
* @throws SAXException 
*/
public static T DeserializeObjects(String xmlFile, String schemaFile, Class type) throws JAXBException, FileNotFoundException, SAXException
{
// Create a JAXB context passing in the class of the object we want to marshal/unmarshal
        JAXBContext context = JAXBContext.newInstance(type);
 
// Create the unmarshaller to transform the XML back into an object
        Unmarshaller unmarshaller = context.createUnmarshaller();
 
        // Create a variable for the schema.
        Schema xmlSchema = null;
        
        if (schemaFile != null)
        {
        // Establish the schema factory for loading the schema
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
       
        // Get a File object representation of the inputed schema
        File sf = new File(schemaFile);
       
        // Use the factory and file objects to instantiate an instance of the Schema object
        xmlSchema = factory.newSchema(sf);
        }
        
        // Set schema validation.  Null value turns validation off.
        unmarshaller.setSchema(xmlSchema);
        
        // Unmarshal the XML in the stringWriter back into an object
        Object o = unmarshaller.unmarshal(new FileInputStream(xmlFile));
        T entity = type.cast(o);
        
return entity;
}
/**
* Converts an instance object into a String containing the XML representation of that object.
* @param entity - The object to serialize into XML
* @return
* @throws JAXBException
*/
public static String SerializeObjects(T entity) throws JAXBException
{
// Create a JAXB context passing in the class of the object we want to marshal/unmarshal
        JAXBContext context = JAXBContext.newInstance(entity.getClass());
 
        // Create the marshaller, this will transform the object into XML
        Marshaller marshaller = context.createMarshaller();
 
        // Create a stringWriter to hold the XML
        StringWriter stringWriter = new StringWriter();
        
        // Marshal the javaObject and write the XML to the stringWriter
        marshaller.marshal(entity, stringWriter);
 
        return stringWriter.toString();
}
}

No comments:

Post a Comment