// prepare SAX result handler
Writer writer = new NoopWriter();
SAXWriter saxHandler = new SAXWriter(writer, "UTF-8"); // SAXWriter implements ContentHandler
saxHandler.setPrettyPrint(true); // enabled formatting
saxHandler.setXMLDeclaration(true); // insert XML declaration to output
Serializer ser = new SAXSerializer(saxHandler);
ser.emit(result); // emit SAX events to SAX handler
for(Item item : result) {// Note that every Sequence instances implement Iterable.
// get string value
String stringValue = item.stringValue();
// get type
Type itemType = item.getType();
if(TypeUtil.subtypeOf(itemType, NodeType.ELEMENT)) {
// the Item is element
} else if(TypeUtil.instanceOf(item, NodeType.TEXT)) {
// another variant
}
// print string value
System.out.println(item); // item.toString() invokes item.stringValue()
// Item is subclass of Sequence, thus it can also be converted to SAX events.
ser.emit(item);
}
// The above 'for each' is equivalent to the following expression.
IFocus focus = result.iterator(); // IFocus extends Iterable
while(result.next(focus)) {