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