Java : DOMImplementation (XML) 示例
DOMImplementation (Java SE 22 & JDK 22) 示例。
您将在大多数 DOMImplementation 方法中找到代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
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)
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)
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)
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)
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