Java XML Node to String getXmlString(Node n)

Here you can find the source of getXmlString(Node n)

Description

get Xml String

License

Open Source License

Declaration

public static String getXmlString(Node n) 

Method Source Code

//package com.java2s;
/*/* w w w.j  av a 2s. c o  m*/
 * Copyright 2006-2007 Javector Software LLC
 *
 * Licensed under the GNU General Public License, Version 2 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 * THE SOURCE CODE AND ACCOMPANYING FILES ARE PROVIDED WITHOUT ANY WARRANTY,
 * WRITTEN OR IMPLIED.
 *
 * The copyright holder provides this software under other licenses for those
 * wishing to include it with products or systems not licensed under the GPL.
 * Contact licenses@javector.com for more information.
 */

import org.w3c.dom.Node;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.StringWriter;

public class Main {
    public static String getXmlString(Node n) {
        Transformer serializer = null;
        try {
            serializer = TransformerFactory.newInstance().newTransformer();
            serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                    "yes");
            StringWriter sw = new StringWriter();
            StreamResult out = new StreamResult(sw);
            serializer.transform(new DOMSource(n), out);
            String result = ((StringWriter) out.getWriter()).getBuffer()
                    .toString();
            return result;
        } catch (TransformerException e) {
            return null;
        }
    }
}

Related

  1. getStringFromXML(Node node)
  2. getText(final Node node)
  3. getXml(Node node)
  4. getXmlAsString(Node node)
  5. getXMLString(Node doc)
  6. getXMLString(Node node, boolean omitXmlDeclaration)
  7. getXMLStringFragmentFromNode(Node node)
  8. nodeToStr(final Node node)
  9. nodeToString(final Node node)