Java XML Node to String nodeToString(Node node, StringBuffer sb)

Here you can find the source of nodeToString(Node node, StringBuffer sb)

Description

node To String

License

Open Source License

Parameter

Parameter Description
node a parameter
sb a parameter

Declaration

public static void nodeToString(Node node, StringBuffer sb) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * 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:/*  www  .  j a  va 2 s.co m*/
 *     Sybase, Inc. - 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 {
    /**
     * @param node
     * @param sb
     */
    public static void nodeToString(Node node, StringBuffer sb) {
        int type = node.getNodeType();
        switch (type) {
        case Node.DOCUMENT_NODE:
            sb.append("<?xml version=\"1.0\" ?>"); //$NON-NLS-1$
            nodeToString(((Document) node).getDocumentElement(), sb);
            break;

        case Node.ELEMENT_NODE:
            sb.append("<"); //$NON-NLS-1$
            sb.append(node.getNodeName());
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                sb.append(" " + attr.getNodeName() + "=\"" //$NON-NLS-1$ //$NON-NLS-2$
                        + attr.getNodeValue() + "\""); //$NON-NLS-1$
            }

            NodeList children = node.getChildNodes();
            if (children != null) {
                int len = children.getLength();
                if (len != 0) {
                    sb.append(">"); //$NON-NLS-1$
                }
                for (int i = 0; i < len; i++) {
                    nodeToString(children.item(i), sb);
                }
            }
            break;

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

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

        case Node.TEXT_NODE:
            sb.append(node.getNodeValue());
            break;

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

        if (type == Node.ELEMENT_NODE) {
            if (node.getFirstChild() != null) {
                sb.append("</"); //$NON-NLS-1$
                sb.append(node.getNodeName());
                sb.append(">"); //$NON-NLS-1$
            } else {
                sb.append("/>"); //$NON-NLS-1$
            }

        }
    }
}

Related

  1. nodeToString(Node node)
  2. nodeToString(Node node)
  3. nodeToString(Node node, boolean pretty)
  4. nodeToString(Node node, Set parentPrefixes, String namespaceURI)
  5. nodeToString(Node node, StringBuffer buf)
  6. nodeToString(Object node)
  7. nodeToString(org.w3c.dom.Node domNode)
  8. nodeToText(Node node)
  9. nodeToWriter(Node node, Writer writer)