Java : DOMImplementationLS (XML) - API使用例
DOMImplementationLS (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
API仕様書のおともにどうぞ。
概要
DOMImplementationLS は、DOM Load and Save(DOMLS) 機能の起点となるインタフェースです。
DOMを文字列に変換するには、よく Transformer が使われます。
ただし、Transformer は汎用的に設計されたAPIのため、DocumentType や EntityReference が思うように変換できないこともあるかもしれません。
一方、DOMLS の LSSerializer を使えば DocumentType や EntityReference をそのまま出力できます。
関連記事 : XML (DOM) の基本操作
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa "bbb">
]>
<root>&aaa;</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
factory.setExpandEntityReferences(false);
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var domImpl = builder.getDOMImplementation();
final var feature = domImpl.getFeature("LS", "3.0");
if (feature instanceof DOMImplementationLS ls) {
final var serializer = ls.createLSSerializer();
final var str = serializer.writeToString(document);
System.out.println(str);
// 結果
// ↓
//<?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root [
//<!ENTITY aaa 'bbb'>
//]>
//<root>&aaa;</root>
}
フィールド
static final short MODE_ASYNCHRONOUS
OpenJDK 22 ではおそらくサポートされていません。
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
System.out.println(domImpl.hasFeature("LS-Async", "3.0")); // false
System.out.println(domImpl.getFeature("LS-Async", "3.0")); // null
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
final var feature = domImpl.getFeature("LS", "3.0");
if (feature instanceof DOMImplementationLS ls) {
try {
final var lsParser = ls.createLSParser(
DOMImplementationLS.MODE_ASYNCHRONOUS, null);
} catch (DOMException e) {
System.out.println(e);
}
// 結果
// ↓
//org.w3c.dom.DOMException: NOT_SUPPORTED_ERR:
// リクエストしたタイプのオブジェクトまたは操作は実装でサポートされていません。
}
static final short MODE_SYNCHRONOUS
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
final var feature = domImpl.getFeature("LS", "3.0");
if (feature instanceof DOMImplementationLS ls) {
final var xml = """
<root>abcd</root>
""";
final var lsParser = ls.createLSParser(
DOMImplementationLS.MODE_SYNCHRONOUS, null);
System.out.println(lsParser.getAsync()); // false
final var lsInput = ls.createLSInput();
lsInput.setStringData(xml);
final var document = lsParser.parse(lsInput);
final var serializer = ls.createLSSerializer();
final var str = serializer.writeToString(document);
System.out.println(str);
// 結果
// ↓
//<?xml version="1.0" encoding="UTF-16"?><root>abcd</root>
}
メソッド
LSInput createLSInput ()
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
final var feature = domImpl.getFeature("LS", "3.0");
if (feature instanceof DOMImplementationLS ls) {
final var xml = """
<root>abcd</root>
""";
final var lsParser = ls.createLSParser(
DOMImplementationLS.MODE_SYNCHRONOUS, null);
System.out.println(lsParser.getAsync()); // false
final var lsInput = ls.createLSInput();
lsInput.setStringData(xml);
final var document = lsParser.parse(lsInput);
final var serializer = ls.createLSSerializer();
final var str = serializer.writeToString(document);
System.out.println(str);
// 結果
// ↓
//<?xml version="1.0" encoding="UTF-16"?><root>abcd</root>
}
LSOutput createLSOutput ()
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
final var feature = domImpl.getFeature("LS", "3.0");
if (feature instanceof DOMImplementationLS ls) {
final var xml = """
<root>abcd</root>
""";
final var lsParser = ls.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
final var lsInput = ls.createLSInput();
lsInput.setStringData(xml);
final var document = lsParser.parse(lsInput);
final var path = Path.of("R:", "java-work", "sample.xml");
try (final var writer = Files.newBufferedWriter(path)) {
final var lsOutput = ls.createLSOutput();
lsOutput.setCharacterStream(writer);
final var serializer = ls.createLSSerializer();
serializer.write(document, lsOutput);
}
System.out.println(Files.readString(path));
// 結果
// ↓
//<?xml version="1.0" encoding="UTF-16"?><root>abcd</root>
}
LSParser createLSParser (short mode, String schemaType)
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
final var feature = domImpl.getFeature("LS", "3.0");
if (feature instanceof DOMImplementationLS ls) {
final var xml = """
<root>abcd</root>
""";
final var lsParser = ls.createLSParser(
DOMImplementationLS.MODE_SYNCHRONOUS, null);
System.out.println(lsParser.getAsync()); // false
final var lsInput = ls.createLSInput();
lsInput.setStringData(xml);
final var document = lsParser.parse(lsInput);
final var serializer = ls.createLSSerializer();
final var str = serializer.writeToString(document);
System.out.println(str);
// 結果
// ↓
//<?xml version="1.0" encoding="UTF-16"?><root>abcd</root>
}
LSSerializer createLSSerializer ()
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();
final var feature = domImpl.getFeature("LS", "3.0");
if (feature instanceof DOMImplementationLS ls) {
final var xml = """
<root>abcd</root>
""";
final var lsParser = ls.createLSParser(
DOMImplementationLS.MODE_SYNCHRONOUS, null);
System.out.println(lsParser.getAsync()); // false
final var lsInput = ls.createLSInput();
lsInput.setStringData(xml);
final var document = lsParser.parse(lsInput);
final var serializer = ls.createLSSerializer();
final var str = serializer.writeToString(document);
System.out.println(str);
// 結果
// ↓
//<?xml version="1.0" encoding="UTF-16"?><root>abcd</root>
}