Java : CharacterData (XML) 示例
CharacterData (Java SE 22 & JDK 22) 示例。
您将在大多数 CharacterData 方法中找到代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
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)
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
}
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
请参阅下面的链接。