Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

XML Unmarshal

By: a guest on Dec 14th, 2011  |  syntax: Java  |  size: 0.87 KB  |  views: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }