Here you can find the source of elementToString(Element element)
Converts an Element object into String representation
Parameter | Description |
---|---|
element | a parameter |
Parameter | Description |
---|---|
TransformerException | an exception |
public static String elementToString(Element element) throws TransformerException
//package com.java2s; /**/*from w w w . ja v a2 s . c om*/ * (C) Copyright 2015 Zaizi Limited (http://www.zaizi.com). * * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser General Public License * (LGPL) version 3.0 which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl-3.0.en.html * * This library 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. * **/ import org.w3c.dom.Element; import javax.xml.transform.OutputKeys; 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 java.io.StringWriter; public class Main { /** * <p>Converts an {@code Element} object into {@code String} representation</p> * @param element * @return * @throws TransformerException */ public static String elementToString(Element element) throws TransformerException { DOMSource domSource = new DOMSource(element); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, streamResult); return stringWriter.toString(); } }