Java XML Element to String getXMLStringFromNode(Element node)

Here you can find the source of getXMLStringFromNode(Element node)

Description

get XML String From Node

License

Apache License

Declaration

public static String getXMLStringFromNode(Element node)
            throws Exception 

Method Source Code

//package com.java2s;
/*//www.  j  a  v a2s.  co  m
 Copyright 2013 Microsoft Open Technologies, Inc.

 Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import java.io.StringWriter;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Element;

public class Main {
    public static String getXMLStringFromNode(Element node)
            throws Exception {
        DOMSource domSource = new DOMSource(node);

        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return result.getWriter().toString();
    }
}

Related

  1. getStringValue(final Element element)
  2. getStringValueByTagName(Element element, String tagName)
  3. getStringValueOfElement(Element elem)
  4. getStringValues(Element from, String elementName)
  5. getXmlString(Element node)
  6. toString(Element e)
  7. toString(Element element)
  8. toString(Element node)
  9. toString(Element xml)