Android Utililty Methods XML Document to String Convert

List of utility methods to do XML Document to String Convert

Description

The list of methods to do XML Document to String Convert are organized into topic(s).

Method

StringtoXML(Document document)
Serializes a DOM Document to XML
DOMImplementationLS domLS = (DOMImplementationLS) implementation;
LSSerializer serializer = domLS.createLSSerializer();
String s = serializer.writeToString(document);
return s;
StringgetXml(Document doc)
get Xml
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.METHOD, "xml");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(
        "{http://xml.apache.org/xslt}indent-amount",
        Integer.toString(2));
StringWriter sw = new StringWriter();
...
intnumResults(Document doc)
num Results
Node results = doc.getDocumentElement();
int res = -1;
try {
    res = Integer.valueOf(results.getAttributes()
            .getNamedItem("count").getNodeValue());
} catch (Exception e) {
    res = -1;
return res;
StringdocumentToString(Node n)
document To String
StringBuilder buffer = new StringBuilder();
if (n == null)
    return "";
if (n instanceof Document) {
    buffer.append(documentToString((n).getFirstChild()));
} else if (n instanceof Element) {
    Element element = (Element) n;
    buffer.append("<");
...
StringgetErrorText(Document xml)
get Error Text
NodeList errorNodes = xml.getElementsByTagName("error");
if (errorNodes.getLength() > 0) {
    Element errorElement = (Element) errorNodes.item(0);
    return getElementText(errorElement);
return null;
StringxmlDocumentToString(Document document)
xml Document To String
return docToString(document.getChildNodes()).trim();
StringxmlToString(Document doc)
xml To String
final StringWriter sw = new StringWriter();
TransformerFactory transformerFactory = TransformerFactory
        .newInstance();
try {
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
} catch (Exception e) {
...