Java XML Document to String xmlDocToString(Document doc)

Here you can find the source of xmlDocToString(Document doc)

Description

xml Doc To String

License

Open Source License

Declaration

public static String xmlDocToString(Document doc) 

Method Source Code

//package com.java2s;
/**//from  w ww . j a v  a2 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.Document;
import org.w3c.dom.Element;

public class Main {
    public static String xmlDocToString(Document doc) {
        return elementToString(doc.getDocumentElement());
    }

    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;

    }
}

Related

  1. writeDocumentToStreamResult(Document document, StreamResult outputTarget)
  2. writeToBytes(Document document)
  3. writeXML(Document d)
  4. writeXmlToString(Document xmlDoc)
  5. xmlDocToString(Document doc)
  6. xmlDocumentDecode(final String xmlURI)
  7. xmlDocumentToString(Document doc)
  8. xmlDocumentToString(Document doc)
  9. xmlDocumentToString(Document doc)