XML Util

Convert XML document to String

protected String docToString(Document doc){
  String xmlStr = null;
  try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult streamResult = new StreamResult(new StringWriter());
    DOMSource domSrc = new DOMSource(doc);
    transformer.transform(domSrc,streamResult);
    xmlStr = streamResult.getWriter().toString();
  } catch (TransformerConfigurationException e) {
    log.error(e);
  } catch (TransformerFactoryConfigurationError e) {
    log.error(e);
  } catch (TransformerException e) {
    log.error(e);
  }
  return xmlStr;
}
This entry was posted in xml. Bookmark the permalink.