Here you can find the source of getString(Node node, boolean indent)
Parameter | Description |
---|---|
node | a parameter |
indent | Indent the XML when formatting |
public static String getString(Node node, boolean indent)
//package com.java2s; /*----------------------------------------------------------------------------- * GDSC SMLM Software/*from www . ja v a2 s. co m*/ * * Copyright (C) 2013 Alex Herbert * Genome Damage and Stability Centre * University of Sussex, UK * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. *---------------------------------------------------------------------------*/ import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Node; public class Main { /** * Return the contents of the node as a string. Any exceptions are ignored and the method returns null. * * @param node * @param indent * Indent the XML when formatting * @return The node contents */ public static String getString(Node node, boolean indent) { Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, (indent) ? "yes" : "no"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(node); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerConfigurationException e) { //e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { //e.printStackTrace(); } catch (TransformerException e) { //e.printStackTrace(); } return ""; } }