private void writeDataElements(Writer out, HashTree sorted) throws IOException {
    XmlSerializer xml = null;
    try {
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      xml = factory.newSerializer();
    } catch (XmlPullParserException xppe) {
      throw new RuntimeException("Couldn't obtain xml serializer", xppe);
    }

    // we need to write our header manually, because we need to specify
    // XML version 1.1
    out.write(DATA_XML_HEADER + NEWLINE + NEWLINE);

    xml.setOutput(out);
    xml.startTag(null, DATA_ELEM);
    xml.ignorableWhitespace(NEWLINE);

    writeDataElementsForNode(xml, sorted, 0);

    xml.endTag(null, DATA_ELEM);
    xml.ignorableWhitespace(NEWLINE);
    xml.endDocument();

    out.flush();
  }
  private void writeChildElement(XmlSerializer xml, String childName, HashTree child, int depth)
      throws IOException {
    childName = childName.substring(0, childName.length() - 1);

    indent(xml, depth);
    xml.startTag(null, NODE_ELEM);
    xml.attribute(null, NAME_ATTR, childName);
    xml.ignorableWhitespace(NEWLINE);

    writeDataElementsForNode(xml, child, depth + 1);

    indent(xml, depth);
    xml.endTag(null, NODE_ELEM);
    xml.ignorableWhitespace(NEWLINE);
  }
  private void writeDataElement(XmlSerializer xml, String name, SimpleData value, int depth)
      throws IOException {
    String elemName = null;
    String text = null;

    if (value instanceof TagData) {
      elemName = TAG_ELEM;

    } else if (value instanceof DateData) {
      elemName = DATE_ELEM;
      Date d = ((DateData) value).getValue();
      if (d != null) text = XMLUtils.saveDate(d);

    } else if (value instanceof NumberData) {
      elemName = NUMBER_ELEM;
      text = numberFormat.format(((NumberData) value).getDouble());

    } else {
      elemName = STRING_ELEM;
      text = value.toString();
    }

    if (elemName != null) {
      indent(xml, depth);
      xml.startTag(null, elemName);
      xml.attribute(null, NAME_ATTR, name);
      if (text != null) xml.text(text);
      xml.endTag(null, elemName);
      xml.ignorableWhitespace(NEWLINE);
    }
  }
 private void indent(XmlSerializer xml, int depth) throws IOException {
   while (depth-- >= 0) xml.ignorableWhitespace(INDENT);
 }