caucho
 


Text nodes, like elements, are children of nodes. The text value is retrieved using getNodeValue().

Text nodes are added using the appendChild() call, like Elements.
Resulting XML
<top>Hello, world</top>
Adding Attributes with the DOM
// Create a new document
Document doc = builder.newDocument();

// Create the top element
Element top = doc.createElement("top");

// Create Text Node
Text text = doc.createTextNode("Hello, world");

// Add it to the top
top.appendChild(text);

As mentioned above, the value of a text node is retrieved using getNodeValue(). The name of a text node is always #text. Normally, you'll either use instanceof or getNodeType() to tell if the node is a text node, but testing the name against #text will work.

The following function recursively prints all the text children of an element. It tests against CharacterData so CDATA sections will also be printed.

void printText(Node node)
{
  for (; node != null; node = node.getNextSibling()) {
    if (node instanceof CharacterData)
      System.out.print(ptr.getNodeValue());

    printText(node.getFirstChild());
  }
}


Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.