Java : CharacterData (XML) with Examples

CharacterData (Java SE 22 & JDK 22) with Examples.
You will find code examples on most CharacterData methods.


Summary

The CharacterData interface extends Node with a set of attributes and methods for accessing character data in the DOM.

Class diagram

Code examples on this page use the printDocument method below.

public void printDocument(Node node) throws TransformerException {
    final var transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    final var result = new StreamResult(new StringWriter());
    transformer.transform(new DOMSource(node), result);

    System.out.println(result.getWriter());
}
final var xml = """
        <root>abcd</root>
        """;

final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var root = document.getDocumentElement();
System.out.println(root); // [root: null]

if (root.getFirstChild() instanceof CharacterData characterData) {
    System.out.println(characterData); // [#text: abcd]

    characterData.setData("XYZ");

    // <root>XYZ</root>
    printDocument(root);
}

Fields declared in Node

ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_PRECEDING, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE

Please see the link below.

Methods

void appendData (String arg)

Append the string to the end of the character data of the node.

final var xml = """
        <root>abcd</root>
        """;

final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var root = document.getDocumentElement();
System.out.println(root); // [root: null]

if (root.getFirstChild() instanceof CharacterData characterData) {
    System.out.println(characterData); // [#text: abcd]

    characterData.appendData("XYZ");

    // <root>abcdXYZ</root>
    printDocument(root);
}

void deleteData (int offset, int count)

Remove a range of 16-bit units from the node.

final var xml = """
        <root>abcdXYZ</root>
        """;

final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var root = document.getDocumentElement();
System.out.println(root); // [root: null]

if (root.getFirstChild() instanceof CharacterData characterData) {
    System.out.println(characterData); // [#text: abcdXYZ]

    characterData.deleteData(0, 4);

    // <root>XYZ</root>
    printDocument(root);

    characterData.deleteData(1, 2);

    // <root>X</root>
    printDocument(root);
}

String getData ()

The character data of the node that implements this interface.

final var xml = """
        <root>abcd</root>
        """;

final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var root = document.getDocumentElement();
System.out.println(root); // [root: null]

if (root.getFirstChild() instanceof CharacterData characterData) {
    System.out.println(characterData); // [#text: abcd]
    System.out.println(characterData.getData()); // abcd
    System.out.println(characterData.getLength()); // 4
}

int getLength ()

The number of 16-bit units that are available through data and the substringData method below.

final var xml = """
        <root>abcd</root>
        """;

final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var root = document.getDocumentElement();
System.out.println(root); // [root: null]

if (root.getFirstChild() instanceof CharacterData characterData) {
    System.out.println(characterData); // [#text: abcd]
    System.out.println(characterData.getData()); // abcd
    System.out.println(characterData.getLength()); // 4
}

void insertData (int offset, String arg)

Insert a string at the specified 16-bit unit offset.

final var xml = """
        <root>XYZ</root>
        """;

final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var root = document.getDocumentElement();
System.out.println(root); // [root: null]

if (root.getFirstChild() instanceof CharacterData characterData) {
    System.out.println(characterData); // [#text: XYZ]

    characterData.insertData(0, "abcd");

    // <root>abcdXYZ</root>
    printDocument(root);

    characterData.insertData(4, "123");

    // <root>abcd123XYZ</root>
    printDocument(root);
}

void replaceData (int offset, int count, String arg)

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

final var xml = """
        <root>012345</root>
        """;

final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var root = document.getDocumentElement();
System.out.println(root); // [root: null]

if (root.getFirstChild() instanceof CharacterData characterData) {
    System.out.println(characterData); // [#text: 012345]

    characterData.replaceData(0, 2, "abcd");

    // <root>abcd2345</root>
    printDocument(root);
}
final var xml = """
        <root>012345</root>
        """;

final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var root = document.getDocumentElement();
System.out.println(root); // [root: null]

if (root.getFirstChild() instanceof CharacterData characterData) {
    System.out.println(characterData); // [#text: 012345]

    characterData.replaceData(4, 2, "XYZ");

    // <root>0123XYZ</root>
    printDocument(root);
}

void setData (String data)

The character data of the node that implements this interface.

final var xml = """
        <root>abcd</root>
        """;

final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var root = document.getDocumentElement();
System.out.println(root); // [root: null]

if (root.getFirstChild() instanceof CharacterData characterData) {
    System.out.println(characterData); // [#text: abcd]

    characterData.setData("XYZ");

    // <root>XYZ</root>
    printDocument(document);
}

String substringData (int offset, int count)

Extracts a range of data from the node.

final var xml = """
        <root>abcd</root>
        """;

final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var root = document.getDocumentElement();
System.out.println(root); // [root: null]

if (root.getFirstChild() instanceof CharacterData characterData) {
    System.out.println(characterData); // [#text: abcd]

    System.out.println(characterData.getData()); // abcd

    System.out.println(characterData.substringData(0, 1)); // a
    System.out.println(characterData.substringData(0, 2)); // ab
    System.out.println(characterData.substringData(0, 3)); // abc
    System.out.println(characterData.substringData(0, 4)); // abcd

    System.out.println(characterData.substringData(1, 3)); // bcd
    System.out.println(characterData.substringData(2, 2)); // cd
    System.out.println(characterData.substringData(3, 1)); // d
}

Methods declared in Node

appendChild, cloneNode, compareDocumentPosition, getAttributes, getBaseURI, getChildNodes, getFeature, getFirstChild, getLastChild, getLocalName, getNamespaceURI, getNextSibling, getNodeName, getNodeType, getNodeValue, getOwnerDocument, getParentNode, getPrefix, getPreviousSibling, getTextContent, getUserData, hasAttributes, hasChildNodes, insertBefore, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, removeChild, replaceChild, setNodeValue, setPrefix, setTextContent, setUserData

Please see the link below.


Related posts

To top of page