Prints the specified node, then prints all of its children XML Element. - Java XML

Java examples for XML:XML Node Child

Description

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

Demo Code

/************************************************************************
 MIT License//from   w w w . j a  v  a 2  s.co  m

 Copyright (c) 2010 University of Connecticut

 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ***********************************************************************/
//package com.java2s;

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 Tutorials