Write Xml with XML Transform - Android XML

Android examples for XML:XML Transform

Description

Write Xml with XML Transform

Demo Code


//package com.java2s;

import java.io.OutputStream;
import java.util.Properties;
import javax.xml.transform.OutputKeys;
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 org.w3c.dom.Document;

public class Main {
    public static void WriteXml(OutputStream stream, Document document)
            throws TransformerException {

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        Properties outFormat = new Properties();
        outFormat.setProperty(OutputKeys.INDENT, "yes");
        outFormat.setProperty(OutputKeys.METHOD, "xml");
        outFormat.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        outFormat.setProperty(OutputKeys.VERSION, "1.0");
        outFormat.setProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperties(outFormat);

        DOMSource domSource = new DOMSource(document.getDocumentElement());
        StreamResult result = new StreamResult(stream);
        transformer.transform(domSource, result);

    }/*  w w w .  j av a 2s  . c o  m*/
}

Related Tutorials