Class XmlSerialiser

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> List<T> collectionFromXml​(Class<T> type, InputStream source)
      Converts the contents of the InputStream to a List with objects of the given class.
      static <T> List<T> collectionFromXml​(Class<T> type, Reader source)
      Converts the contents of the Reader to a List with objects of the given class.
      static <T> List<T> collectionFromXml​(Class<T> type, String source)
      Converts the contents of the string to a List with objects of the given class.
      static <T> List<T> collectionFromXml​(Class<T> type, Source source)
      Converts the contents of the Source to a List with objects of the given class.
      protected static <T> jakarta.xml.bind.JAXBElement<?> createCollectionElement​(String rootName, Collection<T> c)
      Create a JAXBElement containing a JAXBCollection.
      protected static <T> Class<?>[] findTypes​(Collection<T> c)
      Discovers all the classes in the given Collection.
      static <T> String schema​(T obj)  
      static <T> void schema​(T obj, OutputStream schemaDest)  
      static <T> void schema​(T obj, Writer schemaDest)  
      static <T> String toXml​(String rootName, Collection<T> c, boolean format, boolean fragment)
      Convert a collection to a string.
      static <T> void toXml​(String rootName, Collection<T> c, File f, boolean format, boolean fragment)
      Convert a collection to a string and stores it in a File.
      static <T> void toXml​(String rootName, Collection<T> c, OutputStream dest, boolean format, boolean fragment)
      Convert a collection to a string and sends it to the OutputStream.
      static <T> void toXml​(String rootName, Collection<T> c, Writer w, boolean format, boolean fragment)
      Convert a collection to a string and sends it to the Writer.
      static <T> String toXml​(T obj, boolean format, boolean fragment)
      Convert an object to a string.
      static <T> void toXml​(T obj, File f, boolean format, boolean fragment)
      Convert an object to a string and save it to a File.
      static <T> void toXml​(T obj, OutputStream source, boolean format, boolean fragment)
      Convert an object to a string and send it to an OutputStream.
      static <T> void toXml​(T obj, Writer wr, boolean format, boolean fragment)
      Convert an object to a string and send it to a Writer.
      static <T> void toXml​(T obj, XMLStreamWriter wr, boolean format, boolean fragment)
      Convert an object to a string and send it to a Writer.
      static <T,​C>
      C
      typeFromXml​(Class<T> type, File source)
      Convert the contents of a file to an object of a given class.
      static <T,​C>
      C
      typeFromXml​(Class<T> type, InputStream source)
      Convert the contents of an InputStream to an object of a given class.
      static <T,​C>
      C
      typeFromXml​(Class<T> type, Reader source)
      Convert the contents of a Reader to an object of a given class.
      static <T,​C>
      C
      typeFromXml​(Class<T> type, String source)
      Convert a string to an object of a given class.
      static <T,​C>
      C
      typeFromXml​(Class<T> type, Source source)
      Convert the contents of a Source to an object of a given class.
    • Method Detail

      • typeFromXml

        public static <T,​C> C typeFromXml​(Class<T> type,
                                                String source)
                                         throws jakarta.xml.bind.JAXBException
        Convert a string to an object of a given class.
        Parameters:
        type - Type of object
        source - Input string
        Returns:
        Object of the given type
        Throws:
        jakarta.xml.bind.JAXBException
      • typeFromXml

        public static <T,​C> C typeFromXml​(Class<T> type,
                                                File source)
                                         throws jakarta.xml.bind.JAXBException
        Convert the contents of a file to an object of a given class.
        Parameters:
        type - Type of object
        source - File to be read
        Returns:
        Object of the given type
        Throws:
        jakarta.xml.bind.JAXBException
      • typeFromXml

        public static <T,​C> C typeFromXml​(Class<T> type,
                                                Reader source)
                                         throws jakarta.xml.bind.JAXBException
        Convert the contents of a Reader to an object of a given class.
        Parameters:
        type - Type of object
        source - Reader to be read
        Returns:
        Object of the given type
        Throws:
        jakarta.xml.bind.JAXBException
      • typeFromXml

        public static <T,​C> C typeFromXml​(Class<T> type,
                                                InputStream source)
                                         throws jakarta.xml.bind.JAXBException
        Convert the contents of an InputStream to an object of a given class.
        Parameters:
        type - Type of object
        source - InputStream to be read
        Returns:
        Object of the given type
        Throws:
        jakarta.xml.bind.JAXBException
      • typeFromXml

        public static <T,​C> C typeFromXml​(Class<T> type,
                                                Source source)
                                         throws jakarta.xml.bind.JAXBException
        Convert the contents of a Source to an object of a given class.
        Parameters:
        type - Type of object
        source - Source to be used
        Returns:
        Object of the given type
        Throws:
        jakarta.xml.bind.JAXBException
      • collectionFromXml

        public static <T> List<T> collectionFromXml​(Class<T> type,
                                                    String source)
                                             throws jakarta.xml.bind.JAXBException
        Converts the contents of the string to a List with objects of the given class.
        Parameters:
        type - Type to be used
        source - Input string
        Returns:
        List with objects of the given type
        Throws:
        jakarta.xml.bind.JAXBException
      • collectionFromXml

        public static <T> List<T> collectionFromXml​(Class<T> type,
                                                    Reader source)
                                             throws jakarta.xml.bind.JAXBException
        Converts the contents of the Reader to a List with objects of the given class.
        Parameters:
        type - Type to be used
        source - Input
        Returns:
        List with objects of the given type
        Throws:
        jakarta.xml.bind.JAXBException
      • collectionFromXml

        public static <T> List<T> collectionFromXml​(Class<T> type,
                                                    InputStream source)
                                             throws jakarta.xml.bind.JAXBException
        Converts the contents of the InputStream to a List with objects of the given class.
        Parameters:
        type - Type to be used
        source - Input
        Returns:
        List with objects of the given type
        Throws:
        jakarta.xml.bind.JAXBException
      • collectionFromXml

        public static <T> List<T> collectionFromXml​(Class<T> type,
                                                    Source source)
                                             throws jakarta.xml.bind.JAXBException
        Converts the contents of the Source to a List with objects of the given class.
        Parameters:
        type - Type to be used
        source - Input
        Returns:
        List with objects of the given type
        Throws:
        jakarta.xml.bind.JAXBException
      • toXml

        public static <T> String toXml​(T obj,
                                       boolean format,
                                       boolean fragment)
                                throws jakarta.xml.bind.JAXBException
        Convert an object to a string.
        Parameters:
        obj - Object that needs to be serialized / marshalled.
        Returns:
        String representation of obj
        Throws:
        jakarta.xml.bind.JAXBException
      • toXml

        public static <T> void toXml​(T obj,
                                     Writer wr,
                                     boolean format,
                                     boolean fragment)
                              throws jakarta.xml.bind.JAXBException
        Convert an object to a string and send it to a Writer.
        Parameters:
        obj - Object that needs to be serialized / marshalled
        wr - Writer used for outputting the marshalled object
        Throws:
        jakarta.xml.bind.JAXBException
      • toXml

        public static <T> void toXml​(T obj,
                                     XMLStreamWriter wr,
                                     boolean format,
                                     boolean fragment)
                              throws jakarta.xml.bind.JAXBException
        Convert an object to a string and send it to a Writer.
        Parameters:
        obj - Object that needs to be serialized / marshalled
        wr - Writer used for outputting the marshalled object
        Throws:
        jakarta.xml.bind.JAXBException
      • toXml

        public static <T> void toXml​(T obj,
                                     File f,
                                     boolean format,
                                     boolean fragment)
                              throws jakarta.xml.bind.JAXBException
        Convert an object to a string and save it to a File.
        Parameters:
        obj - Object that needs to be serialized / marshalled
        f - Save file
        Throws:
        jakarta.xml.bind.JAXBException
      • toXml

        public static <T> void toXml​(T obj,
                                     OutputStream source,
                                     boolean format,
                                     boolean fragment)
                              throws jakarta.xml.bind.JAXBException
        Convert an object to a string and send it to an OutputStream.
        Parameters:
        obj - Object that needs to be serialized / marshalled
        source - Stream used for output
        Throws:
        jakarta.xml.bind.JAXBException
      • toXml

        public static <T> String toXml​(String rootName,
                                       Collection<T> c,
                                       boolean format,
                                       boolean fragment)
                                throws jakarta.xml.bind.JAXBException
        Convert a collection to a string.
        Parameters:
        rootName - Name of the XML root element
        c - Collection that needs to be marshalled
        Returns:
        String representation of the collection
        Throws:
        jakarta.xml.bind.JAXBException
      • toXml

        public static <T> void toXml​(String rootName,
                                     Collection<T> c,
                                     Writer w,
                                     boolean format,
                                     boolean fragment)
                              throws jakarta.xml.bind.JAXBException
        Convert a collection to a string and sends it to the Writer.
        Parameters:
        rootName - Name of the XML root element
        c - Collection that needs to be marshalled
        w - Output
        Throws:
        jakarta.xml.bind.JAXBException
      • toXml

        public static <T> void toXml​(String rootName,
                                     Collection<T> c,
                                     File f,
                                     boolean format,
                                     boolean fragment)
                              throws jakarta.xml.bind.JAXBException
        Convert a collection to a string and stores it in a File.
        Parameters:
        rootName - Name of the XML root element
        c - Collection that needs to be marshalled
        f - Output file
        Throws:
        jakarta.xml.bind.JAXBException
      • toXml

        public static <T> void toXml​(String rootName,
                                     Collection<T> c,
                                     OutputStream dest,
                                     boolean format,
                                     boolean fragment)
                              throws jakarta.xml.bind.JAXBException
        Convert a collection to a string and sends it to the OutputStream.
        Parameters:
        rootName - Name of the XML root element
        c - Collection that needs to be marshalled
        dest - Output
        Throws:
        jakarta.xml.bind.JAXBException
      • schema

        public static <T> void schema​(T obj,
                                      Writer schemaDest)
                               throws IOException,
                                      jakarta.xml.bind.JAXBException
        Throws:
        IOException
        jakarta.xml.bind.JAXBException
      • schema

        public static <T> void schema​(T obj,
                                      OutputStream schemaDest)
                               throws IOException,
                                      jakarta.xml.bind.JAXBException
        Throws:
        IOException
        jakarta.xml.bind.JAXBException
      • schema

        public static <T> String schema​(T obj)
                                 throws IOException,
                                        jakarta.xml.bind.JAXBException
        Throws:
        IOException
        jakarta.xml.bind.JAXBException
      • findTypes

        protected static <T> Class<?>[] findTypes​(Collection<T> c)
        Discovers all the classes in the given Collection. These need to be in the JAXBContext if you want to marshal those objects. Unfortunatly there's no way of getting the generic type at runtime.
        Parameters:
        c - Collection that needs to be scanned
        Returns:
        Classes found in the collection, including JAXBCollection.
      • createCollectionElement

        protected static <T> jakarta.xml.bind.JAXBElement<?> createCollectionElement​(String rootName,
                                                                                     Collection<T> c)
        Create a JAXBElement containing a JAXBCollection. Needed for marshalling a generic collection without a separate wrapper class.
        Parameters:
        rootName - Name of the XML root element
        c -
        Returns:
        JAXBElement containing the given Collection, wrapped in a JAXBCollection.