Sunday, March 31, 2013

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;
}

No comments:

Post a Comment