Java XML Tutorial - Java CharacterData.getData()








Syntax

CharacterData.getData() has the following syntax.

String getData()  throws DOMException

Example

In the following code shows how to use CharacterData.getData() method.

import java.io.StringReader;
/*from  w w  w.  j a va  2  s.c  o m*/
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class Main {
  public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));
    
    Document doc = factory.newDocumentBuilder().parse(is);

    CDATASection cdataNode = doc.createCDATASection("");

    CharacterData cdata = cdataNode;

    
    cdata.appendData("from java2s.com");
    cdata.deleteData(1, 2);
    System.out.println(cdataNode.getData());
  }
  static String xmlRecords = "<data></data>";
}

The code above generates the following result.