Java XML Node Format formatNode(Node node, int indent, StringBuffer stringBuffer)

Here you can find the source of formatNode(Node node, int indent, StringBuffer stringBuffer)

Description

format Node

License

Open Source License

Declaration

private static StringBuffer formatNode(Node node, int indent, StringBuffer stringBuffer) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009 CollabNet./*from   w ww .j  a v a 2  s.c  om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     CollabNet - initial API and implementation
 ******************************************************************************/

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

public class Main {
    private static StringBuffer formatNode(Node node, StringBuffer stringBuffer) {
        return formatNode(node, 0, stringBuffer);
    }

    private static StringBuffer formatNode(Node node, int indent, StringBuffer stringBuffer) {
        int type = node.getNodeType();
        switch (type) {
        case Node.DOCUMENT_NODE: {
            stringBuffer.append("<?xml version=\"1.0\" ?>\n"); //$NON-NLS-1$
            stringBuffer = formatNode(((Document) node).getDocumentElement(), indent, stringBuffer);
            break;
        }

        case Node.ELEMENT_NODE: {
            if (indent > 0)
                stringBuffer.append("\n"); //$NON-NLS-1$
            for (int i = 0; i < indent; i++)
                stringBuffer.append(" "); //$NON-NLS-1$
            stringBuffer.append("<" + node.getNodeName()); //$NON-NLS-1$
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                stringBuffer.append("\n"); //$NON-NLS-1$
                for (int j = 0; j < indent; j++)
                    stringBuffer.append(" "); //$NON-NLS-1$
                stringBuffer.append("   " + attr.getNodeName().trim() + //$NON-NLS-1$
                        "=\"" + attr.getNodeValue().trim() + //$NON-NLS-1$
                        "\""); //$NON-NLS-1$
            }
            stringBuffer.append(">"); //$NON-NLS-1$

            indent = indent + 3;
            NodeList children = node.getChildNodes();
            if (children != null) {
                int len = children.getLength();
                for (int i = 0; i < len; i++)
                    stringBuffer = formatNode(children.item(i), indent, stringBuffer);
            }
            indent = indent - 3;
            break;
        }

        case Node.ENTITY_REFERENCE_NODE: {
            stringBuffer.append("&"); //$NON-NLS-1$
            stringBuffer.append(node.getNodeName().trim());
            stringBuffer.append(";"); //$NON-NLS-1$
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            stringBuffer.append("<![CDATA["); //$NON-NLS-1$
            stringBuffer.append(node.getNodeValue().trim());
            stringBuffer.append("]]>"); //$NON-NLS-1$
            break;
        }

        case Node.TEXT_NODE: {
            stringBuffer.append("\n"); //$NON-NLS-1$
            for (int i = 0; i < indent; i++)
                stringBuffer.append(" "); //$NON-NLS-1$
            stringBuffer.append(node.getNodeValue().trim());
            break;
        }

        case Node.PROCESSING_INSTRUCTION_NODE: {
            stringBuffer.append("<?"); //$NON-NLS-1$
            stringBuffer.append(node.getNodeName().trim());
            String data = node.getNodeValue().trim();
            {
                stringBuffer.append(" "); //$NON-NLS-1$
                stringBuffer.append(data);
            }
            stringBuffer.append("?>"); //$NON-NLS-1$
            break;
        }
        }

        if (type == Node.ELEMENT_NODE) {
            stringBuffer.append("\n"); //$NON-NLS-1$
            for (int i = 0; i < indent; i++)
                stringBuffer.append(" "); //$NON-NLS-1$
            stringBuffer.append("</"); //$NON-NLS-1$
            stringBuffer.append(node.getNodeName().trim());
            stringBuffer.append('>');
        }
        return stringBuffer;
    }
}

Related

  1. print(PrintStream out, Node node)
  2. printNode(StringBuffer sBuffer, Node node)
  3. printTree(Writer ps, Node node, String indent, String lineSeparator)
  4. renderNode(StringBuffer sb, Node node)