Adding a Text Node to a DOM Document - Java XML

Java examples for XML:DOM

Description

Adding a Text Node to a DOM Document

Demo Code


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class Main {
  public static void main(String[] args) throws Exception {
    Document doc = null;/* ww  w  .  j a v  a2 s. com*/

    // Add a CDATA section to the root element
    Element element = doc.getDocumentElement();
    Text text = doc.createTextNode("data\n");
    element.appendChild(text);

    // Special characters are automatically converted to entities by the XML
    // writers
    text = doc.createTextNode("<>&\"'");
    element.appendChild(text);
  }
}

Related Tutorials