Java : EntityReference (XML) 示例
EntityReference (Java SE 22 & JDK 22) 示例。
您将在大多数 EntityReference 方法中找到代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa "bbb">
<!ENTITY xxx "yyy">
]>
<root/>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var entityReferenceA = document.createEntityReference("aaa");
System.out.println(entityReferenceA); // [aaa: null]
root.appendChild(entityReferenceA);
final var entityReferenceX = document.createEntityReference("xxx");
System.out.println(entityReferenceX); // [xxx: null]
root.appendChild(entityReferenceX);
final var domImpl = builder.getDOMImplementation();
if (domImpl.getFeature("LS", "3.0") instanceof DOMImplementationLS ls) {
final var serializer = ls.createLSSerializer();
final var str = serializer.writeToString(document);
//<?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root [
//<!ENTITY aaa 'bbb'>
//<!ENTITY xxx 'yyy'>
//]>
//<root>&aaa;&xxx;</root>
System.out.println(str);
}
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 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
请参阅下面的链接。