Java XML String Transform writeXmlResult(String xml, Writer w)

Here you can find the source of writeXmlResult(String xml, Writer w)

Description

write Xml Result

License

Open Source License

Declaration

public static void writeXmlResult(String xml, Writer w)
            throws IOException, TransformerException 

Method Source Code

//package com.java2s;
/*/*from  w w  w  .  j a  v  a  2  s.  com*/
 * dalclient library - provides utilities to assist in using KDDart-DAL servers
 * Copyright (C) 2015,2016,2017 Diversity Arrays Technology
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;

import java.io.StringReader;

import java.io.Writer;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    public static final String ENCODING_UTF_8 = "UTF-8";

    public static void writeXmlResult(String xml, Writer w)
            throws IOException, TransformerException {
        StreamSource source = new StreamSource(new StringReader(xml));

        TransformerFactory tf = TransformerFactory.newInstance();

        Transformer transformer = tf.newTransformer();
        transformer
                .setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING_UTF_8);
        transformer.setOutputProperty(
                "{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$

        transformer.transform(source, new StreamResult(w));
    }
}

Related

  1. writeResultToFile(final StreamResult result, final String path)
  2. writeToFile(String xmlContent, String path)
  3. writeXML(Node node, String dtdFilename, String outputFileName)
  4. writeXml(OutputStream os, Node node, String encoding)
  5. writeXmlFile(Element doc, String filename)
  6. xslString(final String xmlFile, final InputStream xslStream)