Java XML Node to String toString(Node node)

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

Description

to String

License

Open Source License

Declaration

public static String toString(Node node) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009 CollabNet.// w  w  w.  j  a  va2s.  co m
 * 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 {
    public static String toString(Node node) {
        return formatNode(node, new StringBuffer()).toString();
    }

    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. toString(final Node xml)
  2. toString(final short nodeType)
  3. toString(Node element)
  4. toString(Node n)
  5. toString(Node n)
  6. toString(Node node)
  7. toString(Node node)
  8. toString(Node node)
  9. toString(Node node)