Java XML Document to Stream dumpDoc(Document domTree, PrintStream out)

Here you can find the source of dumpDoc(Document domTree, PrintStream out)

Description

dump Doc

License

Open Source License

Parameter

Parameter Description
domTree a parameter
out a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static void dumpDoc(Document domTree, PrintStream out)
        throws Exception 

Method Source Code

//package com.java2s;
/*//w  w w.  j a va2  s  . c  o m
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.PrintStream;

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
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.Document;

public class Main {
    /**
     * @param domTree
     * @param out
     * @throws Exception 
     */
    public static void dumpDoc(Document domTree, PrintStream out)
            throws Exception {
        TransformerFactory transformerFact = TransformerFactory
                .newInstance();
        transformerFact.setAttribute("indent-number", 4);
        Transformer transformer = transformerFact.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        //initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(domTree);
        transformer.transform(source, result);
        String xmlString = result.getWriter().toString();
        out.println(xmlString);
    }
}

Related

  1. DocumentToInputStream(Document edoc)
  2. documentToStream(Document document, OutputStream outputStream, String encoding)
  3. dom2InputStream(Document doc)
  4. dumpToStream(Document doc, OutputStream out)
  5. toXml(Document doc, OutputStream out)
  6. write(Document doc, OutputStream out, String enc)
  7. write(Document document, OutputStream byteStream)