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) {
  e.printStackTrace();
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }
 return object;
}

Comments

Popular posts from this blog

PrimeFaces Push with Atmosphere

Creating JSF/CDI Maven project on Eclipse

Adding MyBatis mapper files dynamically