Class XmlUtility


  • public final class XmlUtility
    extends Object
    Helper methods for serializing and de-serializing to XML using JAXB
    Author:
    ramittal
    • Method Detail

      • getXml

        public static <T extends SerializableString getXml​(T entity)
                                                      throws IOException,
                                                             jakarta.xml.bind.JAXBException
        Helper method to serialize an object to XML. Requires object to be Serializable
        Type Parameters:
        T - class that implements Serializable
        Parameters:
        entity - Object to serialize
        Returns:
        String XML representation of object
        Throws:
        IOException - if errors during serialization
        jakarta.xml.bind.JAXBException - if errors during serialization
      • create

        public static <T extends Serializable> T create​(String xml,
                                                        Class<T> classType)
                                                 throws ParserConfigurationException,
                                                        SAXException,
                                                        jakarta.xml.bind.JAXBException
        Helper method to de-serialize XML to an object. Requires object to be Serializable
        Type Parameters:
        T - class that implements Serializable
        Parameters:
        xml - object serialized into valid XML
        classType - Class Type of the object to be de-serialized into
        Returns:
        T De-serialized object
        Throws:
        ParserConfigurationException
        SAXException
        jakarta.xml.bind.JAXBException
      • escapeStringForXml

        public static String escapeStringForXml​(String valueToSerialize)
        Helper method to encode a string to XML Returns the same string if null or empty
        Parameters:
        valueToSerialize - string value to encode into xml
        Returns:
        xml encoded string
      • descapeStringForXml

        public static String descapeStringForXml​(String valueToDeserialize)
        Helper method to decode a string from XML string Returns the same string if null or empty
        Parameters:
        valueToDeserialize - string value to decode from xml
        Returns:
        string decoded from xml
      • getRootElementXml

        public static <T extends SerializableString getRootElementXml​(T entity)
        Removes the XML prologue
        Parameters:
        entity - object to remove prologue
        Returns:
        String String XML representation of root element xml without prologue
      • getRootElementXml

        public static String getRootElementXml​(String xmlString)
        Removes the XML prologue
        Parameters:
        xmlString - string to remove prologue
        Returns:
        String root element xml without prologue