Java XML Document to Writer writeDocument(Document doc, Writer w)

Here you can find the source of writeDocument(Document doc, Writer w)

Description

Writes the given document using the given writer.

License

Apache License

Declaration

public static void writeDocument(Document doc, Writer w)
        throws IOException 

Method Source Code

//package com.java2s;
/*/*  w  w w .j  av  a  2s. c  o m*/

 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 */

import java.io.IOException;

import java.io.Writer;

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 {
    /**
     * Writes the given document using the given writer.
     */
    public static void writeDocument(Document doc, Writer w)
            throws IOException {
        for (Node n = doc.getFirstChild(); n != null; n = n
                .getNextSibling()) {
            writeNode(n, w);
        }
    }

    /**
     * Writes a node using the given writer.
     */
    public static void writeNode(Node n, Writer w) throws IOException {
        switch (n.getNodeType()) {
        case Node.ELEMENT_NODE:
            w.write("<");
            w.write(n.getNodeName());

            if (n.hasAttributes()) {
                NamedNodeMap attr = n.getAttributes();
                int len = attr.getLength();
                for (int i = 0; i < len; i++) {
                    Attr a = (Attr) attr.item(i);
                    w.write(" ");
                    w.write(a.getNodeName());
                    w.write("=\"");
                    w.write(contentToString(a.getNodeValue()));
                    w.write("\"");
                }
            }

            Node c = n.getFirstChild();
            if (c != null) {
                w.write(">");
                for (; c != null; c = c.getNextSibling()) {
                    writeNode(c, w);
                }
                w.write("</");
                w.write(n.getNodeName());
                w.write(">");
            } else {
                w.write("/>");
            }
            break;
        case Node.TEXT_NODE:
            w.write(contentToString(n.getNodeValue()));
            break;
        case Node.CDATA_SECTION_NODE:
            w.write("<![CDATA[");
            w.write(n.getNodeValue());
            w.write("]]>");
            break;
        case Node.ENTITY_REFERENCE_NODE:
            w.write("&");
            w.write(n.getNodeName());
            w.write(";");
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            w.write("<?");
            w.write(n.getNodeName());
            // TD: Bug #19392
            w.write(" ");
            w.write(n.getNodeValue());
            w.write("?>");
            break;
        case Node.COMMENT_NODE:
            w.write("<!--");
            w.write(n.getNodeValue());
            w.write("-->");
            break;
        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType dt = (DocumentType) n;
            w.write("<!DOCTYPE ");
            w.write(n.getOwnerDocument().getDocumentElement().getNodeName());
            String pubID = dt.getPublicId();
            if (pubID != null) {
                w.write(" PUBLIC \"" + dt.getNodeName() + "\" \"" + pubID
                        + "\">");
            } else {
                String sysID = dt.getSystemId();
                if (sysID != null)
                    w.write(" SYSTEM \"" + sysID + "\">");
            }
            break;
        }
        default:
            throw new IOException("Unknown DOM node type "
                    + n.getNodeType());
        }
    }

    /**
     * Returns the given content value transformed to replace invalid
     * characters with entities.
     */
    public static String contentToString(String s) {
        StringBuffer result = new StringBuffer(s.length());

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            switch (c) {
            case '<':
                result.append("&lt;");
                break;
            case '>':
                result.append("&gt;");
                break;
            case '&':
                result.append("&amp;");
                break;
            case '"':
                result.append("&quot;");
                break;
            case '\'':
                result.append("&apos;");
                break;
            default:
                result.append(c);
            }
        }

        return result.toString();
    }
}

Related

  1. toWriter(Document doc, Writer writer)
  2. toXML(Document doc, Writer w, boolean indent)
  3. write(Writer out, Document doc)
  4. writeDocument(Document doc, Writer out)
  5. writeDocument(Document document, Writer writer, Integer indent)
  6. writeDocument(PrintWriter w, Document d)
  7. writeDom(Document doc, Writer w)
  8. writeOutDOM(Document doc, Writer writer)