Java XML Node Print print(Node node)

Here you can find the source of print(Node node)

Description

Print a Node tree recursively.

License

Open Source License

Parameter

Parameter Description
node A DOM tree Node

Return

An xml String representation of the DOM tree.

Declaration

public static String print(Node node) 

Method Source Code

//package com.java2s;
/*//from   w  ww .j  a v a2  s  .c om
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2017 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
 * or LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

import org.w3c.dom.Attr;

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

public class Main {
    /**
     * Print a Node tree recursively.
     * @param node A DOM tree Node
     * @return An xml String representation of the DOM tree.
     */
    public static String print(Node node) {
        if (node == null) {
            return null;
        }

        StringBuffer xml = new StringBuffer(100);
        int type = node.getNodeType();

        switch (type) {
        // print element with attributes
        case Node.ELEMENT_NODE: {
            xml.append('<');
            xml.append(node.getNodeName());

            NamedNodeMap attrs = node.getAttributes();
            int length = attrs.getLength();
            ;

            for (int i = 0; i < length; i++) {
                Attr attr = (Attr) attrs.item(i);
                xml.append(' ');
                xml.append(attr.getNodeName());
                xml.append("=\"");

                //xml.append(normalize(attr.getNodeValue()));
                xml.append(attr.getNodeValue());
                xml.append('"');
            }

            xml.append('>');

            NodeList children = node.getChildNodes();

            if (children != null) {
                int len = children.getLength();

                for (int i = 0; i < len; i++) {
                    xml.append(print(children.item(i)));
                }
            }

            break;
        }

        // handle entity reference nodes
        case Node.ENTITY_REFERENCE_NODE: {
            NodeList children = node.getChildNodes();

            if (children != null) {
                int len = children.getLength();

                for (int i = 0; i < len; i++) {
                    xml.append(print(children.item(i)));
                }
            }

            break;
        }

        // print cdata sections
        case Node.CDATA_SECTION_NODE: {
            xml.append("<![CDATA[");
            xml.append(node.getNodeValue());
            xml.append("]]>");

            break;
        }

        // print text
        case Node.TEXT_NODE: {
            //xml.append(normalize(node.getNodeValue()));
            xml.append(node.getNodeValue());

            break;
        }

        // print processing instruction
        case Node.PROCESSING_INSTRUCTION_NODE: {
            xml.append("<?");
            xml.append(node.getNodeName());

            String data = node.getNodeValue();

            if ((data != null) && (data.length() > 0)) {
                xml.append(' ');
                xml.append(data);
            }

            xml.append("?>");

            break;
        }
        }

        if (type == Node.ELEMENT_NODE) {
            xml.append("</");
            xml.append(node.getNodeName());
            xml.append('>');
        }

        return xml.toString();
    }
}

Related

  1. prettyPrint(Node root)
  2. prettyPrintLoop(Node node, StringBuilder string, String indentation)
  3. prettyPrintNode(Node aNode, int indent)
  4. print(Node node)
  5. printArrayContent(Node[] t)
  6. printDifferences(Node node1, Node node2)
  7. printDOM(Node node)