Java : DOMImplementation (XML) - API使用例
DOMImplementation (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
API仕様書のおともにどうぞ。
概要
DOMImplementationインタフェースは、主に DocumentType を作成することに使われます。
また、DOM Load and Save(DOMLS) 機能の起点となる DOMImplementationLS を取得することができます。
関連記事 : XML (DOM) の基本操作
本記事のAPI使用例では、利便性のために以下の printNode メソッドを共通処理として使います。
public void printNode(DOMImplementation domImpl, Node node) {
final var feature = domImpl.getFeature("LS", "3.0");
if (feature instanceof DOMImplementationLS ls) {
final var serializer = ls.createLSSerializer();
final var str = serializer.writeToString(node);
System.out.println(str);
}
}
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
final var docType = domImpl.createDocumentType(
"root", null, "sample.dtd");
System.out.println(docType); // [root: null]
final var document = domImpl.createDocument(null, "root", docType);
//<?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root SYSTEM "sample.dtd">
//<root/>
printNode(domImpl, document);
メソッド
Document createDocument (String namespaceURI, String qualifiedName, DocumentType doctype)
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
final var docType = domImpl.createDocumentType("root", null, "sample.dtd");
System.out.println(docType); // [root: null]
final var document1 = domImpl.createDocument(null, "root", docType);
//<?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root SYSTEM "sample.dtd">
//<root/>
printNode(domImpl, document1);
final var document2 = domImpl.createDocument("ns", "root", null);
//<?xml version="1.0" encoding="UTF-16"?><root xmlns="ns"/>
printNode(domImpl, document2);
DocumentType createDocumentType (String qualifiedName, String publicId, String systemId)
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
final var docType1 = domImpl.createDocumentType("root", "aaa", "bbb");
// <?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root PUBLIC "aaa" "bbb">
printNode(domImpl, docType1);
final var docType2 = domImpl.createDocumentType("root", "aaa", null);
// <?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root PUBLIC "aaa">
printNode(domImpl, docType2);
final var docType3 = domImpl.createDocumentType("root", null, "bbb");
// <?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root SYSTEM "bbb">
printNode(domImpl, docType3);
final var docType4 = domImpl.createDocumentType("root", null, null);
// <?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root>
printNode(domImpl, docType4);
Object getFeature (String feature, String version)
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
System.out.println(domImpl.hasFeature("LS", "3.0")); // true
final var ret = domImpl.getFeature("LS", "3.0");
System.out.println(ret instanceof DOMImplementationLS); // true
System.out.println(domImpl.hasFeature("LS-Async", "3.0")); // false
System.out.println(domImpl.getFeature("LS-Async", "3.0")); // null
boolean hasFeature (String feature, String version)
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
System.out.println(domImpl.hasFeature("LS", "3.0")); // true
final var ret = domImpl.getFeature("LS", "3.0");
System.out.println(ret instanceof DOMImplementationLS); // true
System.out.println(domImpl.hasFeature("LS-Async", "3.0")); // false
System.out.println(domImpl.getFeature("LS-Async", "3.0")); // null