Java XML JAXB Serialize deserialize(File fXMLFilePath, Class cls)

Here you can find the source of deserialize(File fXMLFilePath, Class cls)

Description

deserialize

License

EUPL

Parameter

Parameter Description
fXMLFilePath a parameter
cls a parameter

Exception

Parameter Description
JAXBException an exception

Declaration

public static Object deserialize(File fXMLFilePath, Class cls) throws JAXBException 

Method Source Code


//package com.java2s;
/*// ww w .ja  v a 2 s .c  o m
 * Copyright 2015, Supreme Court Republic of Slovenia
 * 
 * Licensed under the EUPL, Version 1.1 or ? as soon they will be approved by the European
 * Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work except in
 * compliance with the Licence. You may obtain a copy of the Licence at:
 * 
 * https://joinup.ec.europa.eu/software/page/eupl
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the Licence
 * is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the Licence for the specific language governing permissions and limitations under
 * the Licence.
 */

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.JAXBResult;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Result;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.xml.sax.SAXException;

public class Main {
    /**
     *
     * @param fXMLFilePath
     * @param cls
     * @return
     * @throws JAXBException
     */
    public static Object deserialize(File fXMLFilePath, Class cls) throws JAXBException {
        final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller();
        return um.unmarshal(fXMLFilePath);
    }

    /**
     *
     * @param xml
     * @param cls
     * @return
     * @throws JAXBException
     */
    public static Object deserialize(String xml, Class cls) throws JAXBException {
        final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller();
        return um.unmarshal(new ByteArrayInputStream(xml.getBytes()));
    }

    /**
     *
     * @param io
     * @param cls
     * @return
     * @throws JAXBException
     */
    public static Object deserialize(InputStream io, Class cls) throws JAXBException {
        final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller();
        return um.unmarshal(io);
    }

    /**
     *
     * @param elmnt
     * @param cls
     * @return
     * @throws JAXBException
     */
    public static Object deserialize(Element elmnt, Class cls) throws JAXBException {
        final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller();
        return um.unmarshal(elmnt);
    }

    /**
    *
    * @param elmnt
    * @param xsltSource
    * @param cls
    * @return
    * @throws TransformerConfigurationException
    * @throws JAXBException
    * @throws TransformerException
    */
    public static synchronized Object deserialize(Element elmnt, InputStream xsltSource, Class cls)
            throws TransformerConfigurationException, JAXBException, TransformerException {
        Object obj = null;
        JAXBContext jc = JAXBContext.newInstance(cls);

        if (xsltSource != null) {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer;
            transformer = factory.newTransformer(new StreamSource(xsltSource));

            JAXBResult result = new JAXBResult(jc);
            transformer.transform(new DOMSource(elmnt), result);
            obj = result.getResult();
        } else {
            obj = jc.createUnmarshaller().unmarshal(elmnt);
        }
        return obj;
    }

    /**
     *
     * @param source
     * @param xsltSource
     * @param cls
     * @return
     * @throws TransformerConfigurationException
     * @throws JAXBException
     * @throws TransformerException
     */
    public static synchronized Object deserialize(InputStream source, InputStream xsltSource, Class cls)
            throws TransformerConfigurationException, JAXBException, TransformerException {
        Object obj = null;
        JAXBContext jc = JAXBContext.newInstance(cls);

        if (xsltSource != null) {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer;
            transformer = factory.newTransformer(new StreamSource(xsltSource));

            JAXBResult result = new JAXBResult(jc);
            transformer.transform(new StreamSource(source), result);
            obj = result.getResult();
        } else {
            obj = jc.createUnmarshaller().unmarshal(source);
        }
        return obj;
    }

    /**
     *
     * @param source
     * @param xsltSource
     * @return
     * @throws TransformerConfigurationException
     * @throws JAXBException
     * @throws TransformerException
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static synchronized Document transform(Element source, InputStream xsltSource)
            throws TransformerConfigurationException, JAXBException, TransformerException,
            ParserConfigurationException, SAXException, IOException {
        Document obj = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        if (xsltSource != null) {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer;
            transformer = factory.newTransformer(new StreamSource(xsltSource));
            obj = dbf.newDocumentBuilder().newDocument();
            Result result = new DOMResult(obj);
            transformer.transform(new DOMSource(source), result);
        }
        return obj;
    }
}

Related

  1. deserialize(Class clazz, String filename)
  2. deSerialize(Class docClass, InputStream inputStream)
  3. deserialize(Element elmnt, Class cls)
  4. deserialize(final Class clazz, final String json)
  5. deserialize(Path input, Class clazz)
  6. deserialize(String data, String className)
  7. deserializeFile(String path, Class clazz)