Example usage for org.w3c.dom CharacterData setData

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

Introduction

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

Prototype

public void setData(String data) throws DOMException;

Source Link

Document

The character data of the node that implements this interface.

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.setData("from java2s.com");
    System.out.println(cdataNode.substringData(2, 4));
}

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.setData("from java2s.com");
    System.out.println(cdataNode);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);// www.ja  va2 s . c om
    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("");

    CharacterData cdata = cdataNode;
    cdata = commentNode;
    cdata = textNode;

    cdata.setData("some data");
    int len = cdata.getLength();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from  w w  w.j av  a2  s . c  om*/
    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;

    cdata.setData("some data");
    int len = cdata.getLength();
    int offset = 5;
    len = 4;
    String s = cdata.substringData(offset, len);
}

From source file:org.sakaiproject.tool.assessment.qti.asi.ASIBaseClass.java

protected void wrappingMattext() {
    log.debug("wrappingMattext()");

    try {/*from   w  w w. j  av a2 s.  c  o  m*/
        NodeList list = this.getDocument().getElementsByTagName(QTIConstantStrings.MATTEXT);
        int size = list.getLength();
        for (int i = 0; i < size; i++) {
            Node node = list.item(i);
            Node childNode = node.getFirstChild();
            if ((childNode != null) && childNode instanceof CharacterData) {
                CharacterData cdi = (CharacterData) childNode;
                String data = cdi.getData();

                //modify this string;
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.newDocument();
                Comment comment = doc.createComment(data);
                node.appendChild(node.getOwnerDocument().importNode(comment, true));
                cdi.setData("");
            }
        }
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
    } catch (SAXException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
}