Java XML Tutorial - Java CharacterData .substringData (int offset, int count)








Syntax

CharacterData.substringData(int offset, int count) has the following syntax.

String substringData(int offset,   int count)   throws DOMException

Example

In the following code shows how to use CharacterData.substringData(int offset, int count) method.

import java.io.StringReader;
/*from   ww w.j a  v  a2s . co 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.setData("from java2s.com");
    System.out.println(cdataNode.substringData(2, 4));
  }
  static String xmlRecords = "<data></data>";
}

The code above generates the following result.