Java tutorial
//package com.java2s; //License from project: Open Source License import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.*; import java.io.*; import java.util.Iterator; public class Main { private static void writeAsEncodedUnicode(StartElement element, Writer writer, boolean isEmpty) throws XMLStreamException { try { // Write start tag. writer.write('<'); QName name = element.getName(); String prefix = name.getPrefix(); if (prefix != null && prefix.length() > 0) { writer.write(prefix); writer.write(':'); } writer.write(name.getLocalPart()); // Write namespace declarations. Iterator nsIter = element.getNamespaces(); while (nsIter.hasNext()) { Namespace ns = (Namespace) nsIter.next(); writer.write(' '); ns.writeAsEncodedUnicode(writer); } // Write attributes Iterator attrIter = element.getAttributes(); while (attrIter.hasNext()) { Attribute attr = (Attribute) attrIter.next(); writer.write(' '); attr.writeAsEncodedUnicode(writer); } if (isEmpty) writer.write('/'); writer.write('>'); } catch (IOException ioe) { throw new XMLStreamException(ioe); } } private static void writeAsEncodedUnicode(EndElement element, Writer writer) throws XMLStreamException { try { // Write end tags. writer.write("</"); QName name = element.getName(); String prefix = name.getPrefix(); if (prefix != null && prefix.length() > 0) { writer.write(prefix); writer.write(':'); } writer.write(name.getLocalPart()); writer.write('>'); } catch (IOException ioe) { throw new XMLStreamException(ioe); } } }