Wednesday, January 21, 2009

XML Streaming

// Creates a new reader (potentially recycled).
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(inputStream);

while (reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
switch (reader.next()) {
case XMLStreamConstants.START_ELEMENT:
if (reader.getLocalName().equals("Time")) {
// Reads primitive types (int) attributes directly (no memory allocation).
time.hour = reader.getAttributeValue("hour").toInt();
time.minute = reader.getAttributeValue("minute").toInt();
time.second = reader.getAttributeValue("second").toInt();
}
...
break;
}
}

No comments: