Here you can find the source of loadXML(Object object, String fileNamePath)
public static Object loadXML(Object object, String fileNamePath)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class Main { public static Object loadXML(Object object, String fileNamePath) { Unmarshaller unmarshaller = createUnmarshaller(object); Object loaded = null;/* w ww .j a va 2 s . co m*/ File fileXML = createFile(fileNamePath); if (fileXML.length() > 0) { try { loaded = unmarshaller.unmarshal(new FileReader(fileNamePath)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return loaded; } @SuppressWarnings("rawtypes") private static Unmarshaller createUnmarshaller(Object object) { Class objectClass = object.getClass(); if (objectClass.getSimpleName().equals("Class")) { objectClass = (Class) object; } JAXBContext context; Unmarshaller unmarshaller = null; try { context = JAXBContext.newInstance(objectClass); unmarshaller = context.createUnmarshaller(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } return unmarshaller; } private static File createFile(String fileNamePath) { File file = new File(fileNamePath); if (!file.exists()) { file.getParentFile().mkdirs(); try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return file; } }