Java : DOMImplementationLS (XML) 示例

DOMImplementationLS (Java SE 22 & JDK 22) 示例。
您将在大多数 DOMImplementationLS 方法中找到代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

DOMImplementationLS 包含创建 Load 和 Save 对象的工厂方法。 (机器翻译)

Class diagram

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

创建一个异步 LSParser。 (机器翻译)

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

创建一个同步 LSParser。 (机器翻译)

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 ()

创建一个新的空输入源对象,其中 LSInput.characterStream、LSInput.byteStream、LSInput.stringData、LSInput.systemId、LSInput.publicId、LSInput.baseURI 和 LSInput.encoding 为 null,且 LSInput.certifiedText 为 false。 (机器翻译)

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 ()

创建一个新的空输出目标对象,其中 LSOutput.characterStream、LSOutput.byteStream、LSOutput.systemId 和 LSOutput.encoding 为空。 (机器翻译)

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)

创建一个新的 LSParser。 (机器翻译)

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 ()

创建一个新的 LSSerializer 对象。 (机器翻译)

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>
}

相关文章

To top of page