public void write(Writer writer)
        throws IOException
    {
        if (this.name == null) {
            this.writeEncoded(writer, this.contents);
            return;
        }
        writer.write('<');
        writer.write(this.name);
        if (! this.attributes.isEmpty()) {
            Iterator iter = this.attributes.keySet().iterator();
            while (iter.hasNext()) {
                writer.write(' ');
                String key = (String) iter.next();
                String value = (String) this.attributes.get(key);
                writer.write(key);
                writer.write('='); writer.write('"');
                this.writeEncoded(writer, value);
                writer.write('"');
            }
        }
        if ((this.contents != null) && (this.contents.length() > 0)) {
            writer.write('>');
            this.writeEncoded(writer, this.contents);
            writer.write('<'); writer.write('/');
            writer.write(this.name);
            writer.write('>');
        } else if (this.children.isEmpty()) {
            writer.write('/'); writer.write('>');
        } else {
            writer.write('>');
            Iterator iter = this.iterateChildren();
            while (iter.hasNext()) {
                XMLElement child = (XMLElement) iter.next();
                child.write(writer);
            }
            writer.write('<'); writer.write('/');
            writer.write(this.name);
            writer.write('>');
        }
    }