Java : DocumentBuilderFactory (XML) 示例
DocumentBuilderFactory (Java SE 22 & JDK 22) 示例。
您将在大多数 DocumentBuilderFactory 方法中找到代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
final var xml = """
<root>
<child-a>AAA</child-a>
<child-b>BBB</child-b>
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
System.out.println(childA.getTextContent()); // AAA
final var childB = document.getElementsByTagName("child-b").item(0);
System.out.println(childB); // [child-b: null]
System.out.println(childB.getTextContent()); // BBB
Please see also the link below.
Constructors
DocumentBuilderFactory ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Methods
abstract Object getAttribute (String name)
final var dtdFile = Path.of("R:", "java-work", "sample.dtd");
System.out.println(dtdFile); // R:\java-work\sample.dtd
Files.writeString(dtdFile, """
<!ENTITY aaa "bbb">
""");
final var xml = """
<!DOCTYPE root SYSTEM "file:///R:/java-work/sample.dtd">
<root>&aaa;</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "all");
final var ret = factory.getAttribute(XMLConstants.ACCESS_EXTERNAL_DTD);
System.out.println(ret); // "all"
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
System.out.println(root.getTextContent()); // bbb
}
{
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
final var ret = factory.getAttribute(XMLConstants.ACCESS_EXTERNAL_DTD);
System.out.println(ret); // ""
final var builder = factory.newDocumentBuilder();
try {
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
} catch (SAXException e) {
System.out.println(e);
}
// Result
// ↓
//org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 57; External DTD:
// Failed to read external DTD 'sample.dtd', because 'file' access is not allowed due to
// restriction set by the accessExternalDTD property.
}
abstract boolean getFeature (String name)
// An example of the exponential entity expansion attack.
final var xml = """
<!DOCTYPE root[
<!ENTITY x100 "X">
<!ENTITY x99 "&x100;&x100;">
<!ENTITY x98 "&x99;&x99;">
...
(omitted)
...
<!ENTITY x3 "&x4;&x4;">
<!ENTITY x2 "&x3;&x3;">
<!ENTITY x1 "&x2;&x2;">
]>
<root>&x1;</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
try {
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
} catch (SAXException e) {
System.out.println(e);
}
// Result
// ↓
//org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; JAXP00010001:
// The parser has encountered more than "64000" entity expansions in this document;
// this is the limit imposed by the JDK.
}
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
{
final var ret = factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
// Warning! Entities are growing exponentially, so parsing it takes a very long time.
//final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
}
Schema getSchema ()
final var xsd = """
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root" type="xsd:string"/>
</xsd:schema>
""";
final var schemaFactory = SchemaFactory.newDefaultInstance();
final var schema = schemaFactory.newSchema(
new StreamSource(new ByteArrayInputStream(xsd.getBytes())));
final var factory = DocumentBuilderFactory.newInstance();
System.out.println(factory.getSchema()); // null
factory.setSchema(schema);
System.out.println(factory.getSchema().equals(schema)); // true
final var errorHandler = new DefaultHandler() {
@Override
public void error(SAXParseException e) {
System.out.println("-- ErrorHandler error --");
System.out.println(e);
}
};
{
final var xml = """
<root>abcd</root>
""";
final var builder = factory.newDocumentBuilder();
builder.setErrorHandler(errorHandler);
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
System.out.println(root.getTextContent()); // abcd
}
{
final var xml = """
<root><child>abcd</child></root>
""";
final var builder = factory.newDocumentBuilder();
builder.setErrorHandler(errorHandler);
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
// Result
// ↓
//-- ErrorHandler error --
//org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 33; cvc-type.3.1.2:
// Element 'root' is a simple type, so it must have no element information item [children].
}
boolean isCoalescing ()
final var xml = """
<root>aaa<![CDATA[<&>]]></root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.isCoalescing();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var nodes = root.getChildNodes();
System.out.println("-- nodes --");
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// Result
// ↓
//-- nodes --
//[#text: aaa]
//[#cdata-section: <&>]
}
factory.setCoalescing(true);
{
final var ret = factory.isCoalescing();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var nodes = root.getChildNodes();
System.out.println("-- nodes --");
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// Result
// ↓
//-- nodes --
//[#text: aaa<&>]
}
boolean isExpandEntityReferences ()
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa "bbb">
]>
<root>&aaa;</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.isExpandEntityReferences();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var child = root.getFirstChild();
System.out.println(child); // [#text: bbb]
}
factory.setExpandEntityReferences(false);
{
final var ret = factory.isExpandEntityReferences();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
if (root.getFirstChild() instanceof EntityReference entityReference) {
System.out.println(entityReference); // [aaa: null]
}
}
boolean isIgnoringComments ()
final var xml = """
<root>aaa<!--bbb--></root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.isIgnoringComments();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var nodes = root.getChildNodes();
System.out.println("-- nodes --");
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// Result
// ↓
//-- nodes --
//[#text: aaa]
//[#comment: bbb]
}
factory.setIgnoringComments(true);
{
final var ret = factory.isIgnoringComments();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var nodes = root.getChildNodes();
System.out.println("-- nodes --");
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// Result
// ↓
//-- nodes --
//[#text: aaa]
}
boolean isIgnoringElementContentWhitespace ()
final var xml = """
<!DOCTYPE root [
<!ELEMENT child-a (dummy?)>
<!ELEMENT child-b (#PCDATA)>
]>
<root>
<child-a> </child-a>
<child-b> </child-b>
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.isIgnoringElementContentWhitespace();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
final var childB = document.getElementsByTagName("child-b").item(0);
System.out.println(childB); // [child-b: null]
System.out.println(childA.getFirstChild()); // [#text: ]
System.out.println(childB.getFirstChild()); // [#text: ]
}
factory.setIgnoringElementContentWhitespace(true);
{
final var ret = factory.isIgnoringElementContentWhitespace();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
final var childB = document.getElementsByTagName("child-b").item(0);
System.out.println(childB); // [child-b: null]
System.out.println(childA.getFirstChild()); // null
System.out.println(childB.getFirstChild()); // [#text: ]
}
boolean isNamespaceAware ()
final var xml = """
<ns:root xmlns:ns="sample">
<ns:child/>
</ns:root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.isNamespaceAware();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var child = document.getElementsByTagNameNS("sample", "child").item(0);
System.out.println(child); // null
}
factory.setNamespaceAware(true);
{
final var ret = factory.isNamespaceAware();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var child = document.getElementsByTagNameNS("sample", "child").item(0);
System.out.println(child); // [ns:child: null]
}
boolean isValidating ()
// The XML document intentionally does not match the DTD.
final var xml = """
<!DOCTYPE root [
<!ELEMENT root (child-a)>
]>
<root><child-z/></root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var errorHandler = new DefaultHandler() {
@Override
public void error(SAXParseException e) {
System.out.println("-- ErrorHandler error --");
System.out.println(e);
}
};
{
final var ret = factory.isValidating();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
builder.setErrorHandler(errorHandler);
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var childZ = document.getElementsByTagName("child-z").item(0);
System.out.println(childZ); // [child-z: null]
}
factory.setValidating(true);
{
final var ret = factory.isValidating();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
builder.setErrorHandler(errorHandler);
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
// Result
// ↓
//-- ErrorHandler error --
//org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 17;
// Element type "child-z" must be declared.
//-- ErrorHandler error --
//org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 24;
// The content of element type "root" must match "(child-a)".
}
boolean isXIncludeAware ()
final var sampleFile = Path.of("R:", "java-work", "sample.xml");
System.out.println(sampleFile); // R:\java-work\sample.xml
Files.writeString(sampleFile, """
<child>abcd</child>
""");
final var xml = """
<root xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="file:///R:/java-work/sample.xml" parse="xml" />
</root>
""";
final var factory = DocumentBuilderFactory.newNSInstance();
{
final var ret = factory.isXIncludeAware();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var nodes = document.getElementsByTagName("child");
System.out.println(nodes.getLength()); // 0
}
factory.setXIncludeAware(true);
{
final var ret = factory.isXIncludeAware();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var child = document.getElementsByTagName("child").item(0);
System.out.println(child); // [child: null]
System.out.println(child.getTextContent()); // abcd
}
static DocumentBuilderFactory newDefaultInstance ()
final var xml = """
<root>
<child-a>AAA</child-a>
<child-b>BBB</child-b>
</root>
""";
final var factory = DocumentBuilderFactory.newDefaultInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
System.out.println(childA.getTextContent()); // AAA
final var childB = document.getElementsByTagName("child-b").item(0);
System.out.println(childB); // [child-b: null]
System.out.println(childB.getTextContent()); // BBB
static DocumentBuilderFactory newDefaultNSInstance ()
final var nsFactory = DocumentBuilderFactory.newDefaultNSInstance();
System.out.println(nsFactory.isNamespaceAware()); // true
final var factory = DocumentBuilderFactory.newDefaultInstance();
System.out.println(factory.isNamespaceAware()); // false
abstract DocumentBuilder newDocumentBuilder ()
final var xml = """
<root>
<child-a>AAA</child-a>
<child-b>BBB</child-b>
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
System.out.println(childA.getTextContent()); // AAA
final var childB = document.getElementsByTagName("child-b").item(0);
System.out.println(childB); // [child-b: null]
System.out.println(childB.getTextContent()); // BBB
static DocumentBuilderFactory newInstance ()
final var xml = """
<root>
<child-a>AAA</child-a>
<child-b>BBB</child-b>
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
System.out.println(childA.getTextContent()); // AAA
final var childB = document.getElementsByTagName("child-b").item(0);
System.out.println(childB); // [child-b: null]
System.out.println(childB.getTextContent()); // BBB
static DocumentBuilderFactory newInstance (String factoryClassName, ClassLoader classLoader)
This method is probably for third party libraries. Therefore, the code example is omitted.
static DocumentBuilderFactory newNSInstance ()
final var nsFactory = DocumentBuilderFactory.newNSInstance();
System.out.println(nsFactory.isNamespaceAware()); // true
final var factory = DocumentBuilderFactory.newInstance();
System.out.println(factory.isNamespaceAware()); // false
static DocumentBuilderFactory newNSInstance (String factoryClassName, ClassLoader classLoader)
This method is probably for third party libraries. Therefore, the code example is omitted.
abstract void setAttribute (String name, Object value)
final var dtdFile = Path.of("R:", "java-work", "sample.dtd");
System.out.println(dtdFile); // R:\java-work\sample.dtd
Files.writeString(dtdFile, """
<!ENTITY aaa "bbb">
""");
final var xml = """
<!DOCTYPE root SYSTEM "file:///R:/java-work/sample.dtd">
<root>&aaa;</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "all");
final var ret = factory.getAttribute(XMLConstants.ACCESS_EXTERNAL_DTD);
System.out.println(ret); // "all"
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
System.out.println(root.getTextContent()); // bbb
}
{
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
final var ret = factory.getAttribute(XMLConstants.ACCESS_EXTERNAL_DTD);
System.out.println(ret); // ""
final var builder = factory.newDocumentBuilder();
try {
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
} catch (SAXException e) {
System.out.println(e);
}
// Result
// ↓
//org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 57; External DTD:
// Failed to read external DTD 'sample.dtd', because 'file' access is not allowed due to
// restriction set by the accessExternalDTD property.
}
void setCoalescing (boolean coalescing)
final var xml = """
<root>aaa<![CDATA[<&>]]></root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.isCoalescing();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var nodes = root.getChildNodes();
System.out.println("-- nodes --");
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// Result
// ↓
//-- nodes --
//[#text: aaa]
//[#cdata-section: <&>]
}
factory.setCoalescing(true);
{
final var ret = factory.isCoalescing();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var nodes = root.getChildNodes();
System.out.println("-- nodes --");
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// Result
// ↓
//-- nodes --
//[#text: aaa<&>]
}
void setExpandEntityReferences (boolean expandEntityRef)
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa "bbb">
]>
<root>&aaa;</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.isExpandEntityReferences();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var child = root.getFirstChild();
System.out.println(child); // [#text: bbb]
}
factory.setExpandEntityReferences(false);
{
final var ret = factory.isExpandEntityReferences();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
if (root.getFirstChild() instanceof EntityReference entityReference) {
System.out.println(entityReference); // [aaa: null]
}
}
abstract void setFeature (String name, boolean value)
// An example of the exponential entity expansion attack.
final var xml = """
<!DOCTYPE root[
<!ENTITY x100 "X">
<!ENTITY x99 "&x100;&x100;">
<!ENTITY x98 "&x99;&x99;">
...
(omitted)
...
<!ENTITY x3 "&x4;&x4;">
<!ENTITY x2 "&x3;&x3;">
<!ENTITY x1 "&x2;&x2;">
]>
<root>&x1;</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
try {
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
} catch (SAXException e) {
System.out.println(e);
}
// Result
// ↓
//org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; JAXP00010001:
// The parser has encountered more than "64000" entity expansions in this document;
// this is the limit imposed by the JDK.
}
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
{
final var ret = factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
// Warning! Entities are growing exponentially, so parsing it takes a very long time.
//final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
}
void setIgnoringComments (boolean ignoreComments)
final var xml = """
<root>aaa<!--bbb--></root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.isIgnoringComments();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var nodes = root.getChildNodes();
System.out.println("-- nodes --");
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// Result
// ↓
//-- nodes --
//[#text: aaa]
//[#comment: bbb]
}
factory.setIgnoringComments(true);
{
final var ret = factory.isIgnoringComments();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var nodes = root.getChildNodes();
System.out.println("-- nodes --");
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// Result
// ↓
//-- nodes --
//[#text: aaa]
}
void setIgnoringElementContentWhitespace (boolean whitespace)
final var xml = """
<!DOCTYPE root [
<!ELEMENT child-a (dummy?)>
<!ELEMENT child-b (#PCDATA)>
]>
<root>
<child-a> </child-a>
<child-b> </child-b>
</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.isIgnoringElementContentWhitespace();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
final var childB = document.getElementsByTagName("child-b").item(0);
System.out.println(childB); // [child-b: null]
System.out.println(childA.getFirstChild()); // [#text: ]
System.out.println(childB.getFirstChild()); // [#text: ]
}
factory.setIgnoringElementContentWhitespace(true);
{
final var ret = factory.isIgnoringElementContentWhitespace();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
final var childB = document.getElementsByTagName("child-b").item(0);
System.out.println(childB); // [child-b: null]
System.out.println(childA.getFirstChild()); // null
System.out.println(childB.getFirstChild()); // [#text: ]
}
void setNamespaceAware (boolean awareness)
final var xml = """
<ns:root xmlns:ns="sample">
<ns:child/>
</ns:root>
""";
final var factory = DocumentBuilderFactory.newInstance();
{
final var ret = factory.isNamespaceAware();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var child = document.getElementsByTagNameNS("sample", "child").item(0);
System.out.println(child); // null
}
factory.setNamespaceAware(true);
{
final var ret = factory.isNamespaceAware();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var child = document.getElementsByTagNameNS("sample", "child").item(0);
System.out.println(child); // [ns:child: null]
}
void setSchema (Schema schema)
final var xsd = """
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root" type="xsd:string"/>
</xsd:schema>
""";
final var schemaFactory = SchemaFactory.newDefaultInstance();
final var schema = schemaFactory.newSchema(
new StreamSource(new ByteArrayInputStream(xsd.getBytes())));
final var factory = DocumentBuilderFactory.newInstance();
System.out.println(factory.getSchema()); // null
factory.setSchema(schema);
System.out.println(factory.getSchema().equals(schema)); // true
final var errorHandler = new DefaultHandler() {
@Override
public void error(SAXParseException e) {
System.out.println("-- ErrorHandler error --");
System.out.println(e);
}
};
{
final var xml = """
<root>abcd</root>
""";
final var builder = factory.newDocumentBuilder();
builder.setErrorHandler(errorHandler);
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var root = document.getDocumentElement();
System.out.println(root); // [root: null]
System.out.println(root.getTextContent()); // abcd
}
{
final var xml = """
<root><child>abcd</child></root>
""";
final var builder = factory.newDocumentBuilder();
builder.setErrorHandler(errorHandler);
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
// Result
// ↓
//-- ErrorHandler error --
//org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 33; cvc-type.3.1.2:
// Element 'root' is a simple type, so it must have no element information item [children].
}
void setValidating (boolean validating)
// The XML document intentionally does not match the DTD.
final var xml = """
<!DOCTYPE root [
<!ELEMENT root (child-a)>
]>
<root><child-z/></root>
""";
final var factory = DocumentBuilderFactory.newInstance();
final var errorHandler = new DefaultHandler() {
@Override
public void error(SAXParseException e) {
System.out.println("-- ErrorHandler error --");
System.out.println(e);
}
};
{
final var ret = factory.isValidating();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
builder.setErrorHandler(errorHandler);
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var childZ = document.getElementsByTagName("child-z").item(0);
System.out.println(childZ); // [child-z: null]
}
factory.setValidating(true);
{
final var ret = factory.isValidating();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
builder.setErrorHandler(errorHandler);
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
// Result
// ↓
//-- ErrorHandler error --
//org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 17;
// Element type "child-z" must be declared.
//-- ErrorHandler error --
//org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 24;
// The content of element type "root" must match "(child-a)".
}
void setXIncludeAware (boolean state)
final var sampleFile = Path.of("R:", "java-work", "sample.xml");
System.out.println(sampleFile); // R:\java-work\sample.xml
Files.writeString(sampleFile, """
<child>abcd</child>
""");
final var xml = """
<root xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="file:///R:/java-work/sample.xml" parse="xml" />
</root>
""";
final var factory = DocumentBuilderFactory.newNSInstance();
{
final var ret = factory.isXIncludeAware();
System.out.println(ret); // false
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var nodes = document.getElementsByTagName("child");
System.out.println(nodes.getLength()); // 0
}
factory.setXIncludeAware(true);
{
final var ret = factory.isXIncludeAware();
System.out.println(ret); // true
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final var child = document.getElementsByTagName("child").item(0);
System.out.println(child); // [child: null]
System.out.println(child.getTextContent()); // abcd
}