Java XML Tutorial - Java CharacterData .appendData (String arg)


CharacterData.appendData(String arg) has the following syntax.

void appendData(String arg)  throws DOMException


In the following code shows how to use CharacterData.appendData(String arg) method.

/*www .j a v a  2s  .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;

    int offset = 0;
    cdata.insertData(offset, "a "); 
    cdata.appendData(" b"); 
  static String xmlRecords = "<data></data>";

The code above generates the following result.