Java : DOMImplementationLS (XML) con ejemplos

DOMImplementationLS (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de DOMImplementationLS.

Nota :


Summary

DOMImplementationLS contiene los métodos de fábrica para crear objetos Load y Save. (Traducción automática)

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

Crear un LSParser asincrónico. (Traducción automática)

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

Crear un LSParser sincrónico. (Traducción automática)

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

Cree un nuevo objeto de fuente de entrada vacío donde LSInput.characterStream, LSInput.byteStream, LSInput.stringData, LSInput.systemId, LSInput.publicId, LSInput.baseURI y LSInput.encoding sean nulos y LSInput.certifiedText sea falso. (Traducción automática)

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

Cree un nuevo objeto de destino de salida vacío donde LSOutput.characterStream, LSOutput.byteStream, LSOutput.systemId, LSOutput.encoding sean nulos. (Traducción automática)

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)

Crea un nuevo LSParser. (Traducción automática)

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

Crea un nuevo objeto LSSerializer. (Traducción automática)

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

Related posts

To top of page