Convert node element To String : Node « XML « Java Tutorial

 * JBoss, Home of Professional Open Source
 * Distributable under LGPL license.
 * See terms of license at

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

 * @author <a href="">Ovidiu Feodorov</a>
 * @version <tt>$Revision: 3282 $</tt> $Id: 3282 2007-11-01
 *          15:32:29Z timfox $
public class Utils {
  public static String elementToString(Node n) {

    String name = n.getNodeName();

    short type = n.getNodeType();

    if (Node.CDATA_SECTION_NODE == type) {
      return "<![CDATA[" + n.getNodeValue() + "]]&gt;";

    if (name.startsWith("#")) {
      return "";

    StringBuffer sb = new StringBuffer();

    NamedNodeMap attrs = n.getAttributes();
    if (attrs != null) {
      for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append(

    String textContent = null;
    NodeList children = n.getChildNodes();

    if (children.getLength() == 0) {
      if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent)) {
      } else {
    } else {
      boolean hasValidChildren = false;
      for (int i = 0; i < children.getLength(); i++) {
        String childToString = elementToString(children.item(i));
        if (!"".equals(childToString)) {
          hasValidChildren = true;

      if (!hasValidChildren && ((textContent = XMLUtil.getTextContent(n)) != null)) {


    return sb.toString();

33.29.1.Add a text node to the element
33.29.2.Add a text node to the beginning of the element
33.29.3.Add a text node before the last child of the element
33.29.4.Add a text node in front of the new item element
33.29.5.Adding a Text Node to a DOM Document
33.29.6.Removing a Node from a DOM Document
33.29.7.Remove All nodes
33.29.8.Get the text node
33.29.9.Change a particular node in XML
33.29.10.Add another element after the first child of the root element
33.29.11.Create a new element and move the middle text node to it
33.29.12.Move all children of the element in front of the element
33.29.13.Split the node at the beginning of the word
33.29.14.Compare two DOM Nodes from JBoss
33.29.15.Convert NodeList To Node Array
33.29.16.Convert node element To String
33.29.17.Search our next siblings for a given node
33.29.18.Search earlier siblings for a given node
33.29.19.Returns a first child DOM Node of type ELEMENT_NODE for the specified Node
33.29.20.Search up the tree for a given node
33.29.21.Get the first text node associated with this element
33.29.22.Simplified implementation of a Node from a Document Object Model (DOM)
33.29.23.Returns a list of value for the given node
33.29.24.Returns the value of the child node with the given name
33.29.25.Remove this node from its parent.
33.29.26.Find the first text descendent node of an element
33.29.27.Copies the source tree into the specified place in a destination tree.
33.29.28.DOM helper for root element
33.29.29.Compare two DOM Nodes