Java XML Document to String xmlToString(Document document)

Here you can find the source of xmlToString(Document document)

Description

Convert XML Document to its string representation.

License

Apache License

Parameter

Parameter Description
document for conversion

Exception

Parameter Description
Exception - if DocumentBuilder is not initialized

Return

- string representation of XML

Declaration

public static String xmlToString(Document document) throws Exception 

Method Source Code


//package com.java2s;
/*!//from   www  .j a v  a 2s.  c  o  m
 * AtlantBH Custom Jmeter Components v1.0.0
 * http://www.atlantbh.com/jmeter-components/
 *
 * Copyright 2011, AtlantBH
 *
 * Licensed under the under the Apache License, Version 2.0.
 */

import java.io.*;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.Document;

public class Main {
    private static Transformer transformer;

    /**
     * Convert XML {@link Document} to its string representation.
     *
     * @param document for conversion
     * @return - string representation of XML {@link Document}
     * @throws Exception - if {@link DocumentBuilder} is not initialized
     */
    public static String xmlToString(Document document) throws Exception {
        if (transformer == null) {
            throw new Exception("Transformer is null");
        }
        Source xmlSource = new DOMSource(document);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        transformer.transform(xmlSource, result);
        return stringWriter.toString();
    }
}

Related

  1. XMLDOMtoString(Document document)
  2. xmlToFile(Document doc, String fileNameToWrite)
  3. XMLtoString(Document doc)
  4. xmlToString(Document doc)
  5. xmlToString(Document doc)
  6. xmlToString(Document xml)
  7. xmlToString(Document xml)
  8. XMLToString(Document XMLDocument)
  9. xmltoString(final Document document)