Here you can find the source of domToString(Node domNode, boolean indent)
Parameter | Description |
---|---|
domNode | the DOM node |
indent | indent or not |
public static String domToString(Node domNode, boolean indent)
//package com.java2s; /*//from w ww.j a v a 2s. c o m * eID Identity Provider Project. * Copyright (C) 2010 FedICT. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License version * 3.0 as published by the Free Software Foundation. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, see * http://www.gnu.org/licenses/. */ import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Node; public class Main { /** * Convert specified DOM {@link Node} to a string representation * * @param domNode * the DOM node * @param indent * indent or not * @return the string representation of the DOM node */ public static String domToString(Node domNode, boolean indent) { try { Source source = new DOMSource(domNode); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); transformer.transform(source, result); return stringWriter.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } } }