Java XML String Transform convertResult(StreamResult result, StringWriter writer)

Here you can find the source of convertResult(StreamResult result, StringWriter writer)

Description

convert Result

License

Open Source License

Declaration

private static void convertResult(StreamResult result, StringWriter writer) 

Method Source Code


//package com.java2s;
/*//  w  w w  .j av  a2s .c o  m
 * Copyright (c) 2016 wetransform GmbH
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     wetransform GmbH <http://www.wetransform.to>
 */

import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;

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

public class Main {
    private static void convertResult(StreamResult result, StringWriter writer) {
        ByteArrayOutputStream baos = (ByteArrayOutputStream) result.getOutputStream();
        writer.write(baos.toString());
    }

    private static void convertResult(DOMResult result, StringWriter writer)
            throws TransformerFactoryConfigurationError {
        TransformerFactory txfFactory = TransformerFactory.newInstance();
        Transformer transformer;
        try {
            transformer = txfFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource resultSource = new DOMSource(result.getNode(), result.getSystemId());
            StreamResult printer = new StreamResult(writer);
            transformer.transform(resultSource, printer);
        } catch (TransformerException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

Related

  1. addElementToXml(String xmlFile, String nodeToAdd, String nodeContent)
  2. addNode(String nodeType, String idField, String nodeID, File destFile, ArrayList attributes)
  3. applyXSL(File xmlFile, File xslFile, String outputFilename)
  4. base64encode(String decodedString)
  5. convertPlist(File info_plist_file, String script_url, Map script_parameters)
  6. convertValidatorResult(Result result, StringWriter writer)
  7. createElement(Node node, String name, String value, Map attributes)
  8. createFullReportLog(String sessionLogDir)
  9. createObject(String classname)