Java : DOMImplementation (XML) with Examples

DOMImplementation (Java SE 22 & JDK 22) with Examples.
You will find code examples on most DOMImplementation methods.


Summary

The DOMImplementation interface provides a number of methods for performing operations that are independent of any particular instance of the document object model.

Class diagram

Code examples on this page use the printNode method below.

public void printNode(DOMImplementation domImpl, Node node) {
    final var feature = domImpl.getFeature("LS", "3.0");
    if (feature instanceof DOMImplementationLS ls) {
        final var serializer = ls.createLSSerializer();
        final var str = serializer.writeToString(node);
        System.out.println(str);
    }
}
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();

final var domImpl = builder.getDOMImplementation();
final var docType = domImpl.createDocumentType(
        "root", null, "sample.dtd");
System.out.println(docType); // [root: null]

final var document = domImpl.createDocument(null, "root", docType);

//<?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root SYSTEM "sample.dtd">
//<root/>
printNode(domImpl, document);

Methods

Document createDocument (String namespaceURI, String qualifiedName, DocumentType doctype)

Creates a DOM Document object of the specified type with its document element.

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

final var domImpl = builder.getDOMImplementation();
final var docType = domImpl.createDocumentType("root", null, "sample.dtd");
System.out.println(docType); // [root: null]

final var document1 = domImpl.createDocument(null, "root", docType);

//<?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root SYSTEM "sample.dtd">
//<root/>
printNode(domImpl, document1);

final var document2 = domImpl.createDocument("ns", "root", null);

//<?xml version="1.0" encoding="UTF-16"?><root xmlns="ns"/>
printNode(domImpl, document2);

DocumentType createDocumentType (String qualifiedName, String publicId, String systemId)

Creates an empty DocumentType node.

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

final var docType1 = domImpl.createDocumentType("root", "aaa", "bbb");

// <?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root PUBLIC "aaa" "bbb">
printNode(domImpl, docType1);

final var docType2 = domImpl.createDocumentType("root", "aaa", null);

//  <?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root PUBLIC "aaa">
printNode(domImpl, docType2);

final var docType3 = domImpl.createDocumentType("root", null, "bbb");

// <?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root SYSTEM "bbb">
printNode(domImpl, docType3);

final var docType4 = domImpl.createDocumentType("root", null, null);

// <?xml version="1.0" encoding="UTF-16"?><!DOCTYPE root>
printNode(domImpl, docType4);

Object getFeature (String feature, String version)

This method returns a specialized object which implements the specialized APIs of the specified feature and version, as specified in DOM Features.

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

System.out.println(domImpl.hasFeature("LS", "3.0")); // true

final var ret = domImpl.getFeature("LS", "3.0");
System.out.println(ret instanceof DOMImplementationLS); // true

System.out.println(domImpl.hasFeature("LS-Async", "3.0")); // false
System.out.println(domImpl.getFeature("LS-Async", "3.0")); // null

boolean hasFeature (String feature, String version)

Test if the DOM implementation implements a specific feature and version, as specified in DOM Features.

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

System.out.println(domImpl.hasFeature("LS", "3.0")); // true

final var ret = domImpl.getFeature("LS", "3.0");
System.out.println(ret instanceof DOMImplementationLS); // true

System.out.println(domImpl.hasFeature("LS-Async", "3.0")); // false
System.out.println(domImpl.getFeature("LS-Async", "3.0")); // null

Related posts

To top of page