Here you can find the source of elementToString(Element elt, String prefix)
public static String elementToString(Element elt, String prefix)
//package com.java2s; /**/*w ww .j a va 2 s. c om*/ * Copyright? 2005-2006 INRIA/Universit? Pierre Mend?s-France/Universit? Joseph Fourier. * * O3MiSCID (aka OMiSCID) Software written by Sebastien Pesnel, Dominique * Vaufreydaz, Patrick Reignier, Remi Emonet and Julien Letessier. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import org.w3c.dom.Element; public class Main { public static String elementToString(Element elt) { return elementToString(elt, ""); } public static String elementToString(Element elt, String prefix) { String str = prefix + "<" + elt.getNodeName(); // XERCES // NamedNodeMap nnm = elt.getAttributes(); // for (int i = 0; i < nnm.getLength(); i++) { // str += " " + nnm.item(i).getNodeName() + "=\"" + nnm.item(i).getNodeValue() + "\""; // } // // boolean hasSubElt = false; // NodeList nodeList = elt.getChildNodes(); // String str2 = ""; // for (int i = 0; i < nodeList.getLength(); i++) { // Node cur = nodeList.item(i); // if (cur.getNodeType() == Node.ELEMENT_NODE) { // str2 += elementToString((Element) cur, prefix + " "); // hasSubElt = true; // } else if (cur.getNodeType() == Node.CDATA_SECTION_NODE) { // str2 += prefix + " " + generateCDataSection(cur.getTextContent()); // hasSubElt = true; // } // } // // if (hasSubElt) { // str += ">\r\n" + str2 + prefix + "</" + elt.getNodeName() + ">\r\n"; // } else { // str2 = elt.getTextContent(); // if (str2.equals("")) { // str += "/>\r\n"; // } else { // str += ">" + str2 + "</" + elt.getNodeName() + ">\r\n"; // } // } return str; } }