Java : CharacterData (XML) 示例

CharacterData (Java SE 22 & JDK 22) 示例。
您将在大多数 CharacterData 方法中找到代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

CharacterData 接口扩展了 Node,增加了一组用于访问 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

请参阅下面的链接。

Methods

void appendData (String arg)

将字符串附加到节点字符数据的末尾。 (机器翻译)

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)

从节点中删除一系列 16 位单元。 (机器翻译)

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 ()

实现此接口的节点的字符数据。 (机器翻译)

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 ()

可通过下面的数据和 substringData 方法获取的 16 位单元的数量。 (机器翻译)

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)

在指定的 16 位单位偏移处插入字符串。 (机器翻译)

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)

用指定的字符串替换从指定的 16 位单位偏移量开始的字符。 (机器翻译)

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)

实现此接口的节点的字符数据。 (机器翻译)

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)

从节点提取一定范围的数据。 (机器翻译)

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

请参阅下面的链接。


相关文章

To top of page