Skip to content

Latest commit

 

History

History
38 lines (33 loc) · 1.18 KB

README.md

File metadata and controls

38 lines (33 loc) · 1.18 KB

XML Iteration

This library provides classes for de/serializing collections/streams of objects to XML. It relies heavily on JAXB.

Build Status

Deserialization

XMLIterator

Given an InputStream containing the XML

<fruits>
  <fruit name="orange"/>
  <fruit name="lemon"/>
</fruits>
  • the XML element name fruit and
  • a class Fruit with suitable JAXB annotations,

the XMLIterator streams the InputStream until it finds a closing tag of fruit and deserializes it to an instance of Fruit:

XMLIterator<Fruit> iterator = new XMLIterator<>(inputStream, Fruit.class, "fruit");
while (iterator.hasNext()) {
    System.out.println(iterator.next().getName());
}
for (Fruit fruit : new XMLIterator<>(inputStream, Fruit.class, "fruit")) {
    System.out.println(fruit.getName());
}

will result in the output

orange
lemon

Iteration will occur on calls of hasNext() and continue until the end of the stream is reached.

Beware that if the stream is infinite and does not contain any matching element, the stream will iterate indefinitely!