How to return the root element as a JAXB object from marshaled XML file

public static Object getRootElement(String xmlpath, String package_names){
 Object object = null;
 try {
  JAXBContext context = JAXBContext.newInstance(package_names);
  File file = new File(xmlpath);
  FileInputStream fileInputStream = new FileInputStream(file);
  Unmarshaller unmarshaller = context.createUnmarshaller();
  object = unmarshaller.unmarshal(file);
  if (object instanceof javax.xml.bind.JAXBElement) {
   javax.xml.bind.JAXBElement root = (javax.xml.bind.JAXBElement)object;
   object = root.getValue();
 } catch (JAXBException e) {
 } catch (FileNotFoundException e) {
 return object;


Popular posts from this blog

JavaOne 2015 Session: EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples by Gohar

Spring Core - Different Types of Bean Injection

PrimeFaces Push with Atmosphere