Java XML Node Print prettyPrintLoop(Node node, StringBuilder string, String indentation)

Here you can find the source of prettyPrintLoop(Node node, StringBuilder string, String indentation)

Description

pretty Print Loop

License

Open Source License

Declaration

private static void prettyPrintLoop(Node node, StringBuilder string,
            String indentation) 

Method Source Code

//package com.java2s;
/*/*from   www  . ja va2s  .  c  o  m*/
 // This software is subject to the terms of the Eclipse Public License v1.0
 // Agreement, available at the following URL:
 // http://www.eclipse.org/legal/epl-v10.html.
 // Copyright (C) 2007-2010 Julian Hyde
 // All Rights Reserved.
 // You must accept the terms of that agreement to use this software.
 */

import org.w3c.dom.Attr;

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

public class Main {
    private static void prettyPrintLoop(NodeList nodes,
            StringBuilder string, String indentation) {
        for (int index = 0; index < nodes.getLength(); index++) {
            prettyPrintLoop(nodes.item(index), string, indentation);
        }
    }

    private static void prettyPrintLoop(Node node, StringBuilder string,
            String indentation) {
        if (node == null) {
            return;
        }

        int type = node.getNodeType();
        switch (type) {
        case Node.DOCUMENT_NODE:
            string.append("\n");
            prettyPrintLoop(node.getChildNodes(), string, indentation
                    + "\t");
            break;

        case Node.ELEMENT_NODE:
            string.append(indentation);
            string.append("<");
            string.append(node.getNodeName());

            Attr[] attributes;
            if (node.getAttributes() != null) {
                int length = node.getAttributes().getLength();
                attributes = new Attr[length];
                for (int loopIndex = 0; loopIndex < length; loopIndex++) {
                    attributes[loopIndex] = (Attr) node.getAttributes()
                            .item(loopIndex);
                }
            } else {
                attributes = new Attr[0];
            }

            for (Attr attribute : attributes) {
                string.append(" ");
                string.append(attribute.getNodeName());
                string.append("=\"");
                string.append(attribute.getNodeValue());
                string.append("\"");
            }

            string.append(">\n");

            prettyPrintLoop(node.getChildNodes(), string, indentation
                    + "\t");

            string.append(indentation);
            string.append("</");
            string.append(node.getNodeName());
            string.append(">\n");

            break;

        case Node.TEXT_NODE:
            string.append(indentation);
            string.append(node.getNodeValue().trim());
            string.append("\n");
            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            string.append(indentation);
            string.append("<?");
            string.append(node.getNodeName());
            String text = node.getNodeValue();
            if (text != null && text.length() > 0) {
                string.append(text);
            }
            string.append("?>\n");
            break;

        case Node.CDATA_SECTION_NODE:
            string.append(indentation);
            string.append("<![CDATA[");
            string.append(node.getNodeValue());
            string.append("]]>");
            break;
        }
    }
}

Related

  1. prettyPrint(Node root)
  2. prettyPrintNode(Node aNode, int indent)
  3. print(Node node)
  4. print(Node node)
  5. printArrayContent(Node[] t)