Example usage for org.w3c.dom CharacterData replaceData

List of usage examples for org.w3c.dom CharacterData replaceData

Introduction

In this page you can find the example usage for org.w3c.dom CharacterData replaceData.

Prototype

public void replaceData(int offset, int count, String arg) throws DOMException;

Source Link

Document

Replace the characters starting at the specified 16-bit unit offset with the specified string.

Usage

From source file:Main.java

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");
    System.out.println(cdataNode);
    cdata.replaceData(1, 2, "new");
    System.out.println(cdataNode);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from w  ww .  j  a v  a2  s  .  c o  m*/
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    CDATASection cdataNode = doc.createCDATASection("");
    Comment commentNode = doc.createComment("");
    Text textNode = doc.createTextNode("");

    // All three types of nodes implement the CharacterData interface
    CharacterData cdata = cdataNode;
    cdata = commentNode;
    cdata = textNode;

    // Replace text
    String replacement = "c";
    int offset = 10;
    int len = 6;
    cdata.replaceData(offset, len, replacement);
}