Java : CharacterData (XML) - API使用例
CharacterData (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
API仕様書のおともにどうぞ。
概要
CharacterDataインタフェースは、XMLノードである Text と Comment のベースとなるインタフェースです。
関連記事:XML (DOM) の基本操作
本記事のコード例では、利便性のために以下の printDocument メソッドを使います。
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);
}
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
「Java API 使用例 : Node」をご参照ください。
メソッド
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)
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 ()
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)
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)
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
}
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
「Java API 使用例 : Node」をご参照ください。