static String toXML(Document document, String separator) {
        if (separator == null) {
            separator = "";
        }
        String xml = "";
        for (String key : document.keySet()) {
            Object doc = document.get(key);
            if (doc instanceof Document[]) {
                Document[] docs = (Document[]) doc;
                String temp = separator;
                for (int i=0;i<docs.length;i++) {
                    temp = temp + "<item>" + toXML(docs[i],separator+ " ") + separator + "</item>";
                }
                xml = xml + writeElement(key,temp) + separator;
            } else if (doc instanceof Document) {
                xml = xml + writeElement(key,toXML((Document)doc,separator+ " "));
            } else {
                Object value = document.get(key);
                if (value == null) {
                    xml = xml + writeElement(key,"null");
                } else if (value instanceof Object[]) {
                    xml = xml + writeElement(key,xmlStringArray(toStringArray((Object[])value),separator));
                } else {
                    xml = xml + writeElement(key,value.toString());
                }
            }
        }
        return xml + separator;
    }