SHARE
TWEET

XML Unmarshal

a guest Dec 14th, 2011 44 Never
  1. public List<Person> testeUnmarshal() {
  2.   List<Person> people = new ArrayList<Person>();
  3.   Person p = null;
  4.   try {
  5.     JAXBContext context = JAXBContext.newInstance(Person.class);
  6.     Unmarshaller unmarshaller = context.createUnmarshaller();
  7.     File f = new File(FILE_PATH);
  8.     XMLInputFactory inputFactory = XMLInputFactory.newInstance();
  9.     XMLEventReader eventReader = inputFactory.createXMLEventReader(new FileInputStream(f));
  10.     while (eventReader.hasNext()) {
  11.       XMLEvent event = eventReader.peek();
  12.       if (event.isStartElement()) {
  13.         StartElement start = event.asStartElement();
  14.         if (start.getName().getLocalPart() == "person")) {
  15.           JAXBElement<Person> jax_benef = unmarshaller.unmarshal(eventReader, Person.class);
  16.           p = jax_benef.getValue();
  17.         }
  18.       }
  19.       eventReader.next();
  20.     }
  21.   } catch (Exception e) {
  22.   }
  23.   return persons;
  24. }
RAW Paste Data
Top