Java : DOMImplementationLS (XML) 示例
DOMImplementationLS (Java SE 22 & JDK 22) 示例。
您将在大多数 DOMImplementationLS 方法中找到代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
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);
// Result
// ↓
//<?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root [
//<!ENTITY aaa 'bbb'>
//]>
//<root>&aaa;</root>
}
Fields
static final short MODE_ASYNCHRONOUS
An asynchronous LSParser is probably not supported in 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);
}
// Result
// ↓
//org.w3c.dom.DOMException: NOT_SUPPORTED_ERR:
// The implementation does not support the requested type of object or operation.
}
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);
// Result
// ↓
//<?xml version="1.0" encoding="UTF-16"?><root>abcd</root>
}
Methods
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);
// Result
// ↓
//<?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));
// Result
// ↓
//<?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);
// Result
// ↓
//<?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);
// Result
// ↓
//<?xml version="1.0" encoding="UTF-16"?><root>abcd</root>
}