Java XML Document Format printDocument(Document doc, Writer ps)

Here you can find the source of printDocument(Document doc, Writer ps)

Description

Print the XML document

License

Open Source License

Parameter

Parameter Description
doc The XML document
ps The writer to where the XML document is written

Declaration

public static void printDocument(Document doc, Writer ps) 

Method Source Code


//package com.java2s;
/* /*from  w w w . j  av  a 2  s . com*/
 * Copyright (C) 2016 Bruce Beisel
 *
 * 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.Writer;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;

public class Main {
    /**
     * Print the XML document
     *
     * @param doc  The XML document
     * @param ps  The writer to where the XML document is written
     */
    public static void printDocument(Document doc, Writer ps) {
        try {
            String lineSeparator = System.getProperty("line.separator");

            ps.write("<?xml version=\"1.0\"?>");
            ps.write(lineSeparator);
            ps.write(lineSeparator);

            Element node = doc.getDocumentElement();

            printTree(ps, node, "", lineSeparator);
        } catch (IOException e) {
        }
    }

    /**
     * Print the tree of a node. This method is recursive.
     *
     * @param ps  The writer to where the XML document is written
     * @param node The node with which to start the printing
     * @param indent A string that is printed on each line before any XML
     * @param lineSeparator The string written at the end of each line
     */
    private static void printTree(Writer ps, Node node, String indent, String lineSeparator) throws IOException {
        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE:

            NodeList nodes = node.getChildNodes();

            if (nodes != null) {
                for (int i = 0; i < nodes.getLength(); i++) {
                    printTree(ps, nodes.item(i), "", lineSeparator);
                }
            }
            break;

        case Node.ELEMENT_NODE:
            //
            // Print element and atributes
            //
            String name = node.getNodeName();
            ps.write(indent);
            ps.write("<");
            ps.write(name);

            //
            // Print attributes
            //
            NamedNodeMap attributes = node.getAttributes();

            for (int i = 0; i < attributes.getLength(); i++) {
                Node current = attributes.item(i);
                ps.write(" ");
                ps.write(current.getNodeName());
                ps.write("=\"");
                ps.write(current.getNodeValue());
                ps.write("\"");
            }

            //
            // Recurse on each child
            //
            NodeList children = node.getChildNodes();

            if (children != null) {
                ps.write(">");
                ps.write(lineSeparator);

                for (int i = 0; i < children.getLength(); i++)
                    printTree(ps, children.item(i), indent + "  ", lineSeparator);

                ps.write(indent);
                ps.write("</");
                ps.write(name);
                ps.write(">");
                ps.write(lineSeparator);
            } else {
                ps.write("/>");
                ps.write(lineSeparator);
            }

            break;

        case Node.CDATA_SECTION_NODE:
            ps.write(indent);
            ps.write("<![CDATA[");
            ps.write(node.getNodeValue());
            ps.write("]]>");
            ps.write(lineSeparator);
            break;

        case Node.TEXT_NODE:
            String text = node.getNodeValue().trim();

            if (text.length() > 0) {
                ps.write(indent);
                ps.write(text);
                ps.write(lineSeparator);
            }
            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            break;

        case Node.ENTITY_REFERENCE_NODE:
            break;

        case Node.DOCUMENT_TYPE_NODE:
            break;

        default:
            break;
        }
    }
}

Related

  1. printDoc(Document doc, PrintStream pstrm)
  2. printDoctype(Document doc, PrintStream pstrm)
  3. printDocument(Document d)
  4. printDocument(Document doc, boolean prettyPrint)
  5. printDocument(Document doc, OutputStream out)
  6. printDocument(Document document)
  7. printDocument(Document document)
  8. printDocument(Document document)
  9. printPrettyXML(Document doc)