Java : Entity (XML) 示例
Entity (Java SE 22 & JDK 22) 示例。
您将在大多数 Entity 方法中找到代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
Code examples on this page use the printDocument method below.
public void printDocument(Document document) 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(document), result);
System.out.println(result.getWriter());
}
final var path = Path.of("R:", "java-work", "xxx");
System.out.println(path); // R:\java-work\xxx
Files.writeString(path, "yyy");
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa "bbb">
<!ENTITY xxx SYSTEM "file:///R:/java-work/xxx">
]>
<root>
&aaa;
&xxx;
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// bbb
// yyy
//</root>
printDocument(document);
final var entities = document.getDoctype().getEntities();
if (entities.getNamedItem("aaa") instanceof Entity entity) {
System.out.println(entity); // [aaa: null]
System.out.println(entity.getSystemId()); // null
}
if (entities.getNamedItem("xxx") instanceof Entity entity) {
System.out.println(entity); // [xxx: null]
System.out.println(entity.getSystemId()); // file:///R:/java-work/xxx
}
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
String getInputEncoding ()
final var path = Path.of("R:", "java-work", "xxx");
System.out.println(path); // R:\java-work\xxx
Files.writeString(path, "yyy");
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa "bbb">
<!ENTITY xxx SYSTEM "file:///R:/java-work/xxx">
]>
<root>
&aaa;
&xxx;
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// bbb
// yyy
//</root>
printDocument(document);
final var entities = document.getDoctype().getEntities();
if (entities.getNamedItem("aaa") instanceof Entity entity) {
System.out.println(entity); // [aaa: null]
System.out.println(entity.getInputEncoding()); // null
System.out.println(entity.getSystemId()); // null
}
if (entities.getNamedItem("xxx") instanceof Entity entity) {
System.out.println(entity); // [xxx: null]
System.out.println(entity.getInputEncoding()); // UTF-8
System.out.println(entity.getSystemId()); // file:///R:/java-work/xxx
}
String getNotationName ()
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa SYSTEM "bbb" NDATA ccc>
]>
<root/>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var entities = document.getDoctype().getEntities();
if (entities.getNamedItem("aaa") instanceof Entity entity) {
System.out.println(entity); // [aaa: null]
System.out.println(entity.getSystemId()); // bbb
System.out.println(entity.getNotationName()); // ccc
}
String getPublicId ()
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa PUBLIC "bbb" "ccc">
]>
<root/>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var entities = document.getDoctype().getEntities();
if (entities.getNamedItem("aaa") instanceof Entity entity) {
System.out.println(entity); // [aaa: null]
System.out.println(entity.getPublicId()); // bbb
System.out.println(entity.getSystemId()); // ccc
}
String getSystemId ()
final var path = Path.of("R:", "java-work", "xxx");
System.out.println(path); // R:\java-work\xxx
Files.writeString(path, "yyy");
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa "bbb">
<!ENTITY xxx SYSTEM "file:///R:/java-work/xxx">
]>
<root>
&aaa;
&xxx;
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// bbb
// yyy
//</root>
printDocument(document);
final var entities = document.getDoctype().getEntities();
if (entities.getNamedItem("aaa") instanceof Entity entity) {
System.out.println(entity); // [aaa: null]
System.out.println(entity.getInputEncoding()); // null
System.out.println(entity.getSystemId()); // null
}
if (entities.getNamedItem("xxx") instanceof Entity entity) {
System.out.println(entity); // [xxx: null]
System.out.println(entity.getInputEncoding()); // UTF-8
System.out.println(entity.getSystemId()); // file:///R:/java-work/xxx
}
String getXmlEncoding ()
final var path = Path.of("R:", "java-work", "xxx");
System.out.println(path); // R:\java-work\xxx
Files.writeString(path, """
<?xml version="1.0" encoding="UTF-8"?>\
yyy\
""");
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa "bbb">
<!ENTITY xxx SYSTEM "file:///R:/java-work/xxx">
]>
<root>
&aaa;
&xxx;
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// bbb
// yyy
//</root>
printDocument(document);
final var entities = document.getDoctype().getEntities();
if (entities.getNamedItem("aaa") instanceof Entity entity) {
System.out.println(entity); // [aaa: null]
System.out.println(entity.getSystemId()); // null
System.out.println(entity.getXmlVersion()); // null
System.out.println(entity.getXmlEncoding()); // null
}
if (entities.getNamedItem("xxx") instanceof Entity entity) {
System.out.println(entity); // [xxx: null]
System.out.println(entity.getSystemId()); // file:///R:/java-work/xxx
System.out.println(entity.getXmlVersion()); // 1.0
System.out.println(entity.getXmlEncoding()); // UTF-8
}
final var path = Path.of("R:", "java-work", "aaa");
System.out.println(path); // R:\java-work\aaa
Files.writeString(path, """
<?xml version="1.1" encoding="UTF-16"?>\
bbb\
""", StandardCharsets.UTF_16);
final var xml = """
<?xml version="1.1" encoding="UTF-8"?>
<!DOCTYPE root [
<!ENTITY aaa SYSTEM "file:///R:/java-work/aaa">
]>
<root>
&aaa;
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// bbb
//</root>
printDocument(document);
final var entities = document.getDoctype().getEntities();
if (entities.getNamedItem("aaa") instanceof Entity entity) {
System.out.println(entity); // [aaa: null]
System.out.println(entity.getSystemId()); // file:///R:/java-work/aaa
System.out.println(entity.getXmlVersion()); // 1.1
System.out.println(entity.getXmlEncoding()); // UTF-16
}
String getXmlVersion ()
final var path = Path.of("R:", "java-work", "xxx");
System.out.println(path); // R:\java-work\xxx
Files.writeString(path, """
<?xml version="1.0" encoding="UTF-8"?>\
yyy\
""");
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa "bbb">
<!ENTITY xxx SYSTEM "file:///R:/java-work/xxx">
]>
<root>
&aaa;
&xxx;
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// bbb
// yyy
//</root>
printDocument(document);
final var entities = document.getDoctype().getEntities();
if (entities.getNamedItem("aaa") instanceof Entity entity) {
System.out.println(entity); // [aaa: null]
System.out.println(entity.getSystemId()); // null
System.out.println(entity.getXmlVersion()); // null
System.out.println(entity.getXmlEncoding()); // null
}
if (entities.getNamedItem("xxx") instanceof Entity entity) {
System.out.println(entity); // [xxx: null]
System.out.println(entity.getSystemId()); // file:///R:/java-work/xxx
System.out.println(entity.getXmlVersion()); // 1.0
System.out.println(entity.getXmlEncoding()); // UTF-8
}
final var path = Path.of("R:", "java-work", "aaa");
System.out.println(path); // R:\java-work\aaa
Files.writeString(path, """
<?xml version="1.1" encoding="UTF-16"?>\
bbb\
""", StandardCharsets.UTF_16);
final var xml = """
<?xml version="1.1" encoding="UTF-8"?>
<!DOCTYPE root [
<!ENTITY aaa SYSTEM "file:///R:/java-work/aaa">
]>
<root>
&aaa;
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// bbb
//</root>
printDocument(document);
final var entities = document.getDoctype().getEntities();
if (entities.getNamedItem("aaa") instanceof Entity entity) {
System.out.println(entity); // [aaa: null]
System.out.println(entity.getSystemId()); // file:///R:/java-work/aaa
System.out.println(entity.getXmlVersion()); // 1.1
System.out.println(entity.getXmlEncoding()); // UTF-16
}
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
请参阅下面的链接。