Example usage for javax.xml.stream.events Namespace writeAsEncodedUnicode

List of usage examples for javax.xml.stream.events Namespace writeAsEncodedUnicode

Introduction

In this page you can find the example usage for javax.xml.stream.events Namespace writeAsEncodedUnicode.

Prototype

public void writeAsEncodedUnicode(Writer writer) throws javax.xml.stream.XMLStreamException;

Source Link

Document

This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters.

Usage

From source file:Main.java

private static void writeAsEncodedUnicode(StartElement element, Writer writer, boolean isEmpty)
        throws XMLStreamException {
    try {//  www. j  av a 2  s. com
        // 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);
    }
}