Here you can find the source of toString(Element xml)
private static String toString(Element xml)
//package com.java2s; /*//from ww w .ja v a 2 s .c om * * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * */ 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.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.StringWriter; import java.util.Iterator; public class Main { private static String toString(Element xml) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(xml); transformer.transform(source, result); return result.getWriter().toString(); } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) { throw new RuntimeException("Unable to serialize xml element " + xml, e); } } private static String toString(Iterable<Element> xmlIterable) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); Iterator iterator = xmlIterable.iterator(); DOMSource source; if (iterator.hasNext()) { source = new DOMSource((org.w3c.dom.Node) iterator.next()); transformer.transform(source, result); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } while (iterator.hasNext()) { source = new DOMSource((org.w3c.dom.Node) iterator.next()); transformer.transform(source, result); } System.out.println(result.getWriter().toString()); return result.getWriter().toString(); } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) { throw new RuntimeException("Unable to serialize xml element(s) " + xmlIterable.toString(), e); } } }