Java : DOMImplementation (XML) con ejemplos
DOMImplementation (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de DOMImplementation.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
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