Java : DOMImplementation (XML) 示例

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

注解 :

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

简介

DOMImplementation 接口提供了许多方法来执行独立于文档对象模型的任何特定实例的操作。 (机器翻译)

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)

使用其文档元素创建指定类型的 DOM Document 对象。 (机器翻译)

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)

创建一个空的 DocumentType 节点。 (机器翻译)

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)

此方法返回一个专门的对象,该对象实现指定功能和版本的专门 API,如 DOM 功能中所述。 (机器翻译)

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)

测试 DOM 实现是否实现了特定的功能和版本,如 DOM 功能中所述。 (机器翻译)

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

相关文章

To top of page