Description
Takes the supplied node as the root of an xml document and converts it to a String representation.
License
Open Source License
Parameter
Parameter | Description |
---|
node | to convert to a String |
Exception
Parameter | Description |
---|
TransformerException | an exception |
Return
String XML fragment.
Declaration
public static String getXMLStringFragmentFromNode(Node node) throws TransformerException
Method Source Code
//package com.java2s;
/*/* w ww .j a v a 2s .co m*/
* Copyright (c) 2004-2012 The YAWL Foundation. All rights reserved.
* The YAWL Foundation is a collaboration of individuals and
* organisations who are committed to improving workflow technology.
*
* This file is part of YAWL. YAWL is free software: you can
* redistribute it and/or modify it under the terms of the GNU Lesser
* General Public License as published by the Free Software Foundation.
*
* YAWL is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with YAWL. If not, see <http://www.gnu.org/licenses/>.
*/
import org.w3c.dom.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
public class Main {
/**
* Takes the supplied node as the root of an xml document and converts it to a String representation.
* The xml header is omitted.
*
* @param node to convert to a String
* @return String XML fragment.
* @throws TransformerException
*/
public static String getXMLStringFragmentFromNode(Node node) throws TransformerException {
return getXMLStringFragmentFromNode(node, true);
}
/**
* Takes the supplied node as the root of an xml document and converts it to a String representation.
* The xml header is omitted.
*
* @param node to convert to a String
* @param encoding Target encoding of output XML
* @return String XML fragment.
* @throws TransformerException
*/
public static String getXMLStringFragmentFromNode(Node node, String encoding) throws TransformerException {
return getXMLStringFragmentFromNode(node, false, encoding);
}
/**
* Takes the supplied node as the root of an xml document and converts it to a String representation.
*
* @param node to convert to a String
* @param omitDeclaration set to false to include the <? xml ?> declaration
* @return String XML fragment.
* @throws TransformerException
*/
public static String getXMLStringFragmentFromNode(Node node, boolean omitDeclaration)
throws TransformerException {
return getXMLStringFragmentFromNode(node, omitDeclaration, "UTF-8");
}
/**
* Takes the supplied node as the root of an xml document and converts it to a String representation.
*
* @param node to convert to a String
* @param omitDeclaration set to false to include the <? xml ?> declaration
* @param encoding Target encoding of output XML
* @return String XML fragment.
* @throws TransformerException
*/
public static String getXMLStringFragmentFromNode(Node node, boolean omitDeclaration, String encoding)
throws TransformerException {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource source = new DOMSource(node);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.setOutputProperty("indent", "yes");
if (omitDeclaration)
transformer.setOutputProperty("omit-xml-declaration", "yes");
else
transformer.setOutputProperty("omit-xml-declaration", "no");
transformer.transform(source, new StreamResult(baos));
try {
return baos.toString(encoding);
} catch (UnsupportedEncodingException e) //I would prefer to propagate the exception, but it breaks to many contracts
{
return baos.toString();
}
}
/**
* Takes the supplied node as the root of an xml document and converts it to a String representation.
*
* @param node to convert to a String
* @param omitDeclaration set to false to include the <? xml ?> declaration
* @param collapseEmptyTags set to false to use the long form of xml tags (ie. <a></a>)
* @return String XML fragment.
* @throws TransformerException
*/
public static String getXMLStringFragmentFromNode(Node node, boolean omitDeclaration, boolean collapseEmptyTags)
throws TransformerException {
return getXMLStringFragmentFromNode(node, omitDeclaration, "UTF-8", collapseEmptyTags);
}
/**
* Takes the supplied node as the root of an xml document and converts it to a String representation.
*
* @param node to convert to a String
* @param omitDeclaration set to false to include the <? xml ?> declaration
* @param encoding Target encoding of output XML
* @param collapseEmptyTags set to false to use the long form of xml tags (ie. <a></a>)
* @return String XML fragment.
* @throws TransformerException
*/
public static String getXMLStringFragmentFromNode(Node node, boolean omitDeclaration, String encoding,
boolean collapseEmptyTags) throws TransformerException {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource source = new DOMSource(node);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (!collapseEmptyTags)
transformer.setOutputProperty("method", "xhtml");
transformer.setOutputProperty("indent", "yes");
if (omitDeclaration)
transformer.setOutputProperty("omit-xml-declaration", "yes");
else
transformer.setOutputProperty("omit-xml-declaration", "no");
transformer.transform(source, new StreamResult(baos));
try {
return baos.toString(encoding);
} catch (UnsupportedEncodingException e) //I would prefer to propagate the exception, but it breaks to many contracts
{
return baos.toString();
}
}
}
Related
- getXml(Node node)
- getXmlAsString(Node node)
- getXMLString(Node doc)
- getXmlString(Node n)
- getXMLString(Node node, boolean omitXmlDeclaration)
- nodeToStr(final Node node)
- nodeToString(final Node node)
- nodeToString(final Node node)
- nodeToString(final Node node)