Java XML Node Save writeDOM(Node node, PrintWriter writer)

Here you can find the source of writeDOM(Node node, PrintWriter writer)

Description

Recursively write the XML DOM representation into a print writer output stream.

License

Open Source License

Declaration

public static void writeDOM(Node node, PrintWriter writer) 

Method Source Code

//package com.java2s;
// for Our Notice and the LICENSE file for the GNU Lesser General Public

import java.io.PrintWriter;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    private final static String EOL = "\n";

    /**//from   www  .j a v a  2s.co  m
     * Recursively write the XML DOM representation into a print writer
     * output stream.  The DOM tree is output without any formatting.
     */
    public static void writeDOM(Node node, PrintWriter writer) {
        if (node != null) {
            switch (node.getNodeType()) {

            /*
             * Insert the XML header for a document node, including the
             * document type if it exists.
             */
            case Node.DOCUMENT_NODE:
                writer.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + EOL);

                DocumentType doctype = ((Document) node).getDoctype();
                if (doctype != null) {
                    writer.print("<!DOCTYPE ");
                    writer.print(doctype.getName());
                    String public_id = doctype.getPublicId();
                    String system_id = doctype.getSystemId();
                    if (public_id != null) {
                        writer.print(" PUBLIC \"");
                        writer.print(public_id);
                        writer.print("\" \"");
                    } else {
                        writer.print(" SYSTEM \"");
                    }
                    if (system_id != null) {
                        writer.print(system_id);
                    }
                    writer.print("\">" + EOL);
                }

                writeDOM(((Document) node).getDocumentElement(), writer);
                break;

            /*
             * Append an opening element tag, any attributes, children,
             * and a closing element tag.
             */
            case Node.ELEMENT_NODE:
                writer.print("<");
                writer.print(node.getNodeName());

                NamedNodeMap attrs = node.getAttributes();
                int length = (attrs == null ? 0 : attrs.getLength());
                for (int i = 0; i < length; i++) {
                    Attr attr = (Attr) attrs.item(i);
                    writer.print(" ");
                    writer.print(attr.getName());
                    writer.print("=\"");
                    writer.print(encodeXMLString(attr.getValue()));
                    writer.print("\"");
                }

                Node c = node.getFirstChild();
                if (c == null) {
                    writer.print("/>");
                } else {
                    writer.print(">");
                    while (c != null) {
                        writeDOM(c, writer);
                        c = c.getNextSibling();
                    }
                    writer.print("</" + node.getNodeName() + ">");
                }
                break;

            /*
             * Append entity references as "&entity-name;".
             */
            case Node.ENTITY_REFERENCE_NODE:
                writer.print("&");
                writer.print(node.getNodeName());
                writer.print(";");
                break;

            /*
             * Append normalized string data
             */
            case Node.CDATA_SECTION_NODE:
            case Node.TEXT_NODE:
                writer.print(encodeXMLString(node.getNodeValue()));
                break;

            /*
             * Ignore all other node types.
             */
            default:
                break;
            }
        }
    }

    /**
     * Normalize an XML string by replacing less than, greater than,
     * ampersand, and double quote by their XML entity representatives.
     */
    public static String encodeXMLString(String s) {
        StringBuffer buffer = new StringBuffer();

        int length = (s == null ? 0 : s.length());
        for (int i = 0; i < length; i++) {
            char ch = s.charAt(i);
            switch (ch) {
            case '<':
                buffer.append("&lt;");
                break;
            case '>':
                buffer.append("&gt;");
                break;
            case '&':
                buffer.append("&amp;");
                break;
            case '"':
                buffer.append("&quot;");
                break;
            default:
                buffer.append(ch);
                break;
            }
        }

        return buffer.toString();
    }
}

Related

  1. save(String filename, Node node)
  2. toResultSet(Node node, ResultSet rs)
  3. write(Node node, Writer out)
  4. writeByDom(Node node, Writer writer, int indent)
  5. writeDescription(PrintWriter out, Node node, String offSet)
  6. writeNode(Node n, FileOutputStream o)
  7. writeNode(Node n, Writer w)
  8. writeNode(Node node, OutputStream os, boolean formatted)
  9. writeNode(Node node, OutputStream output, Map serializerParams)