private void writeXML(Writer writer,Element element,StringBuilder indentation,boolean isInline) throws IOException {
  if (!isInline) {
    writer.write(indentation.toString());
  }
  if (element.isTextElement()) {
    writer.write(encodeIfRequired(element.getText()));
  }
 else {
    writer.write('<');
    String elementName=encodeIfRequired(element.getTagName());
    writer.write(elementName);
    Hashtable attributes=element.getAttributes();
    if (attributes != null) {
      for (Enumeration keys=attributes.keys(); keys.hasMoreElements(); ) {
        String attributeKey=(String)keys.nextElement();
        String attributeValue=(String)attributes.get(attributeKey);
        writer.write(' ');
        writer.write(encodeIfRequired(attributeKey));
        writer.write("=\"");
        writer.write(encodeIfRequired(attributeValue));
        writer.write('"');
      }
    }
    if (element.isEmpty()) {
      writer.write(" />");
    }
 else {
      writer.write('>');
      if (!isInline && !element.hasTextChild()) {
        writer.write('\n');
        indentation.append('\t');
        for (        Object child : element) {
          if (child instanceof Element) {
            writeXML(writer,(Element)child,indentation,isInline);
          }
 else {
            throw new IllegalStateException("Element contained child of invalid type");
          }
          writer.write('\n');
        }
      }
 else {
        isInline=true;
        for (        Object child : element) {
          if (child instanceof Element) {
            writeXML(writer,(Element)child,null,isInline);
          }
 else {
            throw new IllegalStateException("Element contained child of invalid type");
          }
        }
      }
      if (!isInline) {
        indentation.deleteCharAt(indentation.length() - 1);
        writer.write(indentation.toString());
      }
      writer.write("</");
      writer.write(elementName);
      writer.write('>');
    }
  }
}

private void writeXML ( Writer writer , Element element , StringBuilder indentation , boolean isInline ) throws IOException { if ( ! isInline ) { writer . write ( indentation . toString ( ) ) ; } if ( element . isTextElement ( ) ) { writer . write ( encodeIfRequired ( element . getText ( ) ) ) ; } else { writer . write ( '<' ) ; String elementName = encodeIfRequired ( element . getTagName ( ) ) ; writer . write ( elementName ) ; Hashtable attributes = element . getAttributes ( ) ; if ( attributes != null ) { for ( Enumeration keys = attributes . keys ( ) ; keys . hasMoreElements ( ) ; ) { String attributeKey = ( String ) keys . nextElement ( ) ; String attributeValue = ( String ) attributes . get ( attributeKey ) ; writer . write ( ' ' ) ; writer . write ( encodeIfRequired ( attributeKey ) ) ; writer . write ( "=\"" ) ; writer . write ( encodeIfRequired ( attributeValue ) ) ; writer . write ( '"' ) ; } } if ( element . isEmpty ( ) ) { writer . write ( " />" ) ; } else { writer . write ( '>' ) ; if ( ! isInline && ! element . hasTextChild ( ) ) { writer . write ( '\n' ) ; indentation . append ( '\t' ) ; for ( Object child : element ) { if ( child instanceof Element ) { writeXML ( writer , ( Element ) child , indentation , isInline ) ; } else { throw new IllegalStateException ( "Element contained child of invalid type" ) ; } writer . write ( '\n' ) ; } } else { isInline = true ; for ( Object child : element ) { if ( child instanceof Element ) { writeXML ( writer , ( Element ) child , null , isInline ) ; } else { throw new IllegalStateException ( "Element contained child of invalid type" ) ; } } } if ( ! isInline ) { indentation . deleteCharAt ( indentation . length ( ) - 1 ) ; writer . write ( indentation . toString ( ) ) ; } writer . write ( "</" ) ; writer . write ( elementName ) ; writer . write ( '>' ) ; } } }
writes the xml of an element to a writer using a given starting indentation . note : may output invalid xml if you created text elements using un - escaped strings .
