Java : DOMImplementationLS (XML) - API Examples

DOMImplementationLS (Java SE 17 & JDK 17) API Examples.
You will find code examples on most DOMImplementationLS methods.


Summary

DOMImplementationLS contains the factory methods for creating Load and Save objects.

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

Create an asynchronous LSParser.

An asynchronous LSParser is probably not supported in OpenJDK 17.

final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var domImpl = builder.getDOMImplementation();

final var feature = domImpl.getFeature("LS-Async", "3.0");
System.out.println(feature); // 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

Create a synchronous LSParser.

Please see createLSInput().

Methods

LSInput createLSInput ()

Create a new empty input source object where LSInput.characterStream, LSInput.byteStream , LSInput.stringData LSInput.systemId, LSInput.publicId, LSInput.baseURI, and LSInput.encoding are null, and LSInput.certifiedText is 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 ()

Create a new empty output destination object where LSOutput.characterStream, LSOutput.byteStream, LSOutput.systemId, LSOutput.encoding are 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) {

    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)

Create a new LSParser.

Please see createLSInput().

LSSerializer createLSSerializer ()

Create a new LSSerializer object.

Please see createLSInput().


Related posts

To top of page