Java XML JAXB Serialize deserializeFromXmlFile(T defaultObject, String fileName)

Here you can find the source of deserializeFromXmlFile(T defaultObject, String fileName)

Description

deserialize From Xml File

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <T> T deserializeFromXmlFile(T defaultObject, String fileName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.FileInputStream;

import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T deserializeFromXmlFile(T defaultObject, String fileName) {
        T object = defaultObject;// w w  w  .j a  v  a  2s  .  c o m
        try {
            JAXBContext context = JAXBContext.newInstance(defaultObject.getClass());
            try (FileInputStream inStream = new FileInputStream(fileName)) {
                object = (T) context.createUnmarshaller().unmarshal(inStream);
            }
        } catch (JAXBException | IOException e) {
        }
        return object;
    }
}

Related

  1. deserialize(File fXMLFilePath, Class cls)
  2. deserialize(final Class clazz, final String json)
  3. deserialize(Path input, Class clazz)
  4. deserialize(String data, String className)
  5. deserializeFile(String path, Class clazz)
  6. deserializeXmlToJava(String valueType, Serializable value)
  7. save(Class confClass, Object confObj, File xmlFile)
  8. save(File file, T obj, Class... clazz)
  9. saveDataToFile(File file, T data)