Here you can find the source of parse(XMLStreamReader reader)
public static String parse(XMLStreamReader reader)
//package com.java2s; /*//from ww w .j a v a2 s . co m * Copyright LGPL3 * YES Technology Association * http://yestech.org * * http://www.opensource.org/licenses/lgpl-3.0.html */ import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; public class Main { public static String parse(XMLStreamReader reader) { StringBuffer result = new StringBuffer(); if (reader != null) { try { while (reader.hasNext()) { switch (reader.getEventType()) { case XMLStreamConstants.START_ELEMENT: result.append("<"); printName(reader, result); printNamespaces(reader, result); printAttributes(reader, result); result.append(">"); break; case XMLStreamConstants.END_ELEMENT: result.append("</"); printName(reader, result); result.append(">"); break; case XMLStreamConstants.SPACE: case XMLStreamConstants.CHARACTERS: int start = reader.getTextStart(); int length = reader.getTextLength(); result.append(new String(reader.getTextCharacters(), start, length)); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: result.append("<?"); if (reader.hasText()) result.append(reader.getText()); result.append("?>"); break; case XMLStreamConstants.CDATA: result.append("<![CDATA["); start = reader.getTextStart(); length = reader.getTextLength(); result.append(new String(reader.getTextCharacters(), start, length)); result.append("]]>"); break; case XMLStreamConstants.COMMENT: result.append("<!--"); if (reader.hasText()) result.append(reader.getText()); result.append("-->"); break; case XMLStreamConstants.ENTITY_REFERENCE: result.append(reader.getLocalName()).append("="); if (reader.hasText()) result.append("[").append(reader.getText()).append("]"); break; case XMLStreamConstants.START_DOCUMENT: result.append("<?xml"); result.append(" version='").append(reader.getVersion()).append("'"); result.append(" encoding='").append(reader.getCharacterEncodingScheme()).append("'"); if (reader.isStandalone()) result.append(" standalone='yes'"); else result.append(" standalone='no'"); result.append("?>"); break; } reader.next(); } // end while } catch (XMLStreamException e) { throw new RuntimeException(e); } finally { try { reader.close(); } catch (XMLStreamException e) { } } } return result.toString(); } private static void printName(XMLStreamReader xmlr, StringBuffer result) { if (xmlr.hasName()) { String prefix = xmlr.getPrefix(); String uri = xmlr.getNamespaceURI(); String localName = xmlr.getLocalName(); printName(prefix, uri, localName, result); } } private static void printName(String prefix, String uri, String localName, StringBuffer result) { if (uri != null && !("".equals(uri))) result.append("['").append(uri).append("']:"); if (prefix != null) result.append(prefix).append(":"); if (localName != null) result.append(localName); } private static void printNamespaces(XMLStreamReader xmlr, StringBuffer result) { for (int i = 0; i < xmlr.getNamespaceCount(); i++) { printNamespace(xmlr, i, result); } } private static void printAttributes(XMLStreamReader xmlr, StringBuffer result) { for (int i = 0; i < xmlr.getAttributeCount(); i++) { printAttribute(xmlr, i, result); } } private static void printNamespace(XMLStreamReader xmlr, int index, StringBuffer result) { String prefix = xmlr.getNamespacePrefix(index); String uri = xmlr.getNamespaceURI(index); result.append(" "); if (prefix == null) result.append("xmlns='").append(uri).append("'"); else result.append("xmlns:").append(prefix).append("='").append(uri).append("'"); } private static void printAttribute(XMLStreamReader xmlr, int index, StringBuffer result) { String prefix = xmlr.getAttributePrefix(index); String namespace = xmlr.getAttributeNamespace(index); String localName = xmlr.getAttributeLocalName(index); String value = xmlr.getAttributeValue(index); result.append(" "); printName(prefix, namespace, localName, result); result.append("='").append(value).append("'"); } }