Here you can find the source of nodeToString(Node node, StringBuffer sb)
Parameter | Description |
---|---|
node | a parameter |
sb | a parameter |
public static void nodeToString(Node node, StringBuffer sb)
//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$ } } } }