Adding a CDATA Section to a DOM Document - Java XML

Java examples for XML:CDATA

Description

Adding a CDATA Section to a DOM Document

Demo Code


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

public class Main {
  public static void m() {
    // Obtain an XML document; this method is implemented in
    Document doc = null;/*w ww.  j  a  v a  2s  . co m*/

    // Add a CDATA section to the root element
    Element element = doc.getDocumentElement();
    CDATASection cdata = doc.createCDATASection("data");
    element.appendChild(cdata);

    // If "]]>" appears in the text, two CDATA sections will be written out
    cdata = doc.createCDATASection("more]]>data");
    element.appendChild(cdata);
  }
}

Related Tutorials