Java tutorial
//package com.java2s; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; public class Main { public static void dumpXML(XMLStreamReader parser) throws XMLStreamException { int depth = 0; do { switch (parser.getEventType()) { case XMLStreamConstants.START_ELEMENT: for (int i = 1; i < depth; ++i) { System.out.print(" "); } System.out.print("<"); System.out.print(parser.getLocalName()); for (int i = 0; i < parser.getAttributeCount(); ++i) { System.out.print(" "); System.out.print(parser.getAttributeLocalName(i)); System.out.print("=\""); System.out.print(parser.getAttributeValue(i)); System.out.print("\""); } System.out.println(">"); ++depth; break; case XMLStreamConstants.END_ELEMENT: --depth; for (int i = 1; i < depth; ++i) { System.out.print(" "); } System.out.print("</"); System.out.print(parser.getLocalName()); System.out.println(">"); break; case XMLStreamConstants.CHARACTERS: for (int i = 1; i < depth; ++i) { System.out.print(" "); } System.out.println(parser.getText()); break; } if (depth > 0) parser.next(); } while (depth > 0); } }