Java XML Node Print printDOM(Node node)

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

Description

Prints the specified node, then prints all of its children.

License

Open Source License

Declaration

public static void printDOM(Node node) 

Method Source Code

//package com.java2s;
/**/*from   w  ww.j  a v a 2 s.co m*/
 * This file is part of JEMMA - http://jemma.energy-home.org
 * (C) Copyright 2014 Telecom Italia (http://www.telecomitalia.it)
 *
 * JEMMA is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License (LGPL) version 3
 * or later as published by the Free Software Foundation, which accompanies
 * this distribution and is available at http://www.gnu.org/licenses/lgpl.html
 *
 * JEMMA 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 Lesser General Public License (LGPL) for more details.
 *
 */

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

public class Main {
    /** Prints the specified node, then prints all of its children. */
    public static void printDOM(Node node) {
        int type = node.getNodeType();
        switch (type) {
        // print the document element
        case Node.DOCUMENT_NODE: {
            System.out.println("<?xml version=\"1.0\" ?>");
            printDOM(((Document) node).getDocumentElement());
            break;
        }

        // print element with attributes
        case Node.ELEMENT_NODE: {
            System.out.print("<");
            System.out.print(node.getNodeName());
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                System.out.print(" " + attr.getNodeName().trim() + "=\"" + attr.getNodeValue().trim() + "\"");
            }
            System.out.println(">");

            NodeList children = node.getChildNodes();
            if (children != null) {
                int len = children.getLength();
                for (int i = 0; i < len; i++)
                    printDOM(children.item(i));
            }

            break;
        }

        // handle entity reference nodes
        case Node.ENTITY_REFERENCE_NODE: {
            System.out.print("&");
            System.out.print(node.getNodeName().trim());
            System.out.print(";");
            break;
        }

        // print cdata sections
        case Node.CDATA_SECTION_NODE: {
            System.out.print("<![CDATA[");
            System.out.print(node.getNodeValue().trim());
            System.out.print("]]>");
            break;
        }

        // print text
        case Node.TEXT_NODE: {
            System.out.print(node.getNodeValue().trim());
            break;
        }

        // print processing instruction
        case Node.PROCESSING_INSTRUCTION_NODE: {
            System.out.print("<?");
            System.out.print(node.getNodeName().trim());
            String data = node.getNodeValue().trim();
            {
                System.out.print(" ");
                System.out.print(data);
            }
            System.out.print("?>");
            break;
        }
        }

        if (type == Node.ELEMENT_NODE) {
            System.out.println();
            System.out.print("</");
            System.out.print(node.getNodeName().trim());
            System.out.print('>');
        }
    }
}

Related

  1. prettyPrintNode(Node aNode, int indent)
  2. print(Node node)
  3. print(Node node)
  4. printArrayContent(Node[] t)
  5. printDifferences(Node node1, Node node2)
  6. printDOM(Node node, String prefix)
  7. printDOMTree(Node node, PrintStream out)
  8. printElement(Node node, int level)
  9. printlnCommon(Node n)