Java : Node (XML) with Examples
Node (Java SE 22 & JDK 22) with Examples.
You will find code examples on most Node methods.
Summary
The Node interface is the primary datatype for the entire Document Object Model. It represents a single node in the document tree.
Code examples on this page use the printDocument method below.
public void printDocument(Document document) throws TransformerException {
final var transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
final var result = new StreamResult(new StringWriter());
transformer.transform(new DOMSource(document), result);
System.out.print(result.getWriter());
}
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.newDocument();
final var root = document.createElement("root");
System.out.println(root); // [root: null]
document.appendChild(root);
final var child = document.createElement("child");
System.out.println(child); // [child: null]
root.appendChild(child);
final var text = document.createTextNode("abcd");
System.out.println(text); // [#text: abcd]
child.appendChild(text);
//<root>
// <child>abcd</child>
//</root>
printDocument(document);
Fields
static final short ATTRIBUTE_NODE
The node is an Attr.
final var xml = """
<root attr="abc"/>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node attr = document.getDocumentElement().getAttributes().getNamedItem("attr");
System.out.println(attr); // attr="abc"
System.out.println(Node.ATTRIBUTE_NODE); // 2
System.out.println(attr.getNodeType() == Node.ATTRIBUTE_NODE); // true
static final short CDATA_SECTION_NODE
The node is a CDATASection.
final var xml = """
<root><![CDATA[<&>]]></root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node cdata = document.getDocumentElement().getFirstChild();
System.out.println(cdata); // [#cdata-section: <&>]
System.out.println(Node.CDATA_SECTION_NODE); // 4
System.out.println(cdata.getNodeType() == Node.CDATA_SECTION_NODE); // true
static final short COMMENT_NODE
The node is a Comment.
final var xml = """
<root><!-- abc --></root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node comment = document.getDocumentElement().getFirstChild();
System.out.println(comment); // [#comment: abc ]
System.out.println(Node.COMMENT_NODE); // 8
System.out.println(comment.getNodeType() == Node.COMMENT_NODE); // true
static final short DOCUMENT_FRAGMENT_NODE
The node is a DocumentFragment.
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.newDocument();
final Node fragment = document.createDocumentFragment();
System.out.println(fragment); // [#document-fragment: null]
System.out.println(Node.DOCUMENT_FRAGMENT_NODE); // 11
System.out.println(fragment.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE); // true
static final short DOCUMENT_NODE
The node is a Document.
final var xml = """
<root/>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final Node document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
System.out.println(document); // [#document: null]
System.out.println(Node.DOCUMENT_NODE); // 9
System.out.println(document.getNodeType() == Node.DOCUMENT_NODE); // true
static final short DOCUMENT_POSITION_CONTAINED_BY
The node is contained by the reference node.
final var xml = """
<root><child/></root>
""";
final var builder = DocumentBuilderFactory.newInstance().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); // [child: null]
final var type = root.compareDocumentPosition(child);
System.out.printf("%#x%n", type); // 0x14
System.out.printf("%#x%n", Node.DOCUMENT_POSITION_CONTAINED_BY); // 0x10
System.out.printf("%#x%n", type & Node.DOCUMENT_POSITION_CONTAINED_BY); // 0x10
static final short DOCUMENT_POSITION_CONTAINS
The node contains the reference node.
final var xml = """
<root><child/></root>
""";
final var builder = DocumentBuilderFactory.newInstance().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); // [child: null]
final var type = child.compareDocumentPosition(root);
System.out.printf("%#x%n", type); // 0xa
System.out.printf("%#x%n", Node.DOCUMENT_POSITION_CONTAINS); // 0x8
System.out.printf("%#x%n", type & Node.DOCUMENT_POSITION_CONTAINS); // 0x8
static final short DOCUMENT_POSITION_DISCONNECTED
The two nodes are disconnected.
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document1 = builder.newDocument();
final var node1 = document1.createTextNode("abcd");
System.out.println(node1); // [#text: abcd]
final var document2 = builder.newDocument();
final var node2 = document2.createTextNode("xyz");
System.out.println(node2); // [#text: xyz]
final var type = node1.compareDocumentPosition(node2);
System.out.printf("%#x%n", type); // 0x23
System.out.printf("%#x%n", Node.DOCUMENT_POSITION_DISCONNECTED); // 0x1
System.out.printf("%#x%n", type & Node.DOCUMENT_POSITION_DISCONNECTED); // 0x1
static final short DOCUMENT_POSITION_FOLLOWING
The node follows the reference node.
final var xml = """
<root>
<child-a/>
<child-b/>
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().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]
final var type = childA.compareDocumentPosition(childB);
System.out.printf("%#x%n", type); // 0x4
System.out.printf("%#x%n", Node.DOCUMENT_POSITION_FOLLOWING); // 0x4
System.out.printf("%#x%n", type & Node.DOCUMENT_POSITION_FOLLOWING); // 0x4
static final short DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
The determination of preceding versus following is implementation-specific.
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document1 = builder.newDocument();
final var node1 = document1.createTextNode("abcd");
System.out.println(node1); // [#text: abcd]
final var document2 = builder.newDocument();
final var node2 = document2.createTextNode("xyz");
System.out.println(node2); // [#text: xyz]
final short type = node1.compareDocumentPosition(node2);
System.out.printf("%#x%n", type); // 0x23
System.out.printf("%#x%n", Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); // 0x20
System.out.printf("%#x%n", type & Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); // 0x20
static final short DOCUMENT_POSITION_PRECEDING
The second node precedes the reference node.
final var xml = """
<root>
<child-a/>
<child-b/>
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().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]
final var type = childB.compareDocumentPosition(childA);
System.out.printf("%#x%n", type); // 0x2
System.out.printf("%#x%n", Node.DOCUMENT_POSITION_PRECEDING); // 0x2
System.out.printf("%#x%n", type & Node.DOCUMENT_POSITION_PRECEDING); // 0x2
static final short DOCUMENT_TYPE_NODE
The node is a DocumentType.
final var xml = """
<!DOCTYPE root>
<root/>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node doctype = document.getDoctype();
System.out.println(doctype); // [root: null]
System.out.println(Node.DOCUMENT_TYPE_NODE); // 10
System.out.println(doctype.getNodeType() == Node.DOCUMENT_TYPE_NODE); // true
static final short ELEMENT_NODE
The node is an Element.
final var xml = """
<root/>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node element = document.getDocumentElement();
System.out.println(element); // [root: null]
System.out.println(Node.ELEMENT_NODE); // 1
System.out.println(element.getNodeType() == Node.ELEMENT_NODE); // true
static final short ENTITY_NODE
The node is an Entity.
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa "bbb">
]>
<root>&aaa;</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node entity = document.getDoctype().getEntities().item(0);
System.out.println(entity); // [aaa: null]
System.out.println(Node.ENTITY_NODE); // 6
System.out.println(entity.getNodeType() == Node.ENTITY_NODE); // true
//<root>bbb</root>
printDocument(document);
static final short ENTITY_REFERENCE_NODE
The node is an EntityReference.
final var xml = """
<!DOCTYPE root [
<!ENTITY aaa "bbb">
]>
<root>&aaa;</root>
""";
final var factory = DocumentBuilderFactory.newInstance();
factory.setExpandEntityReferences(false);
final var document = factory.newDocumentBuilder().parse(
new ByteArrayInputStream(xml.getBytes()));
final Node entityReference = document.getDocumentElement().getFirstChild();
System.out.println(entityReference); // [aaa: null]
System.out.println(Node.ENTITY_REFERENCE_NODE); // 5
System.out.println(entityReference.getNodeType() == Node.ENTITY_REFERENCE_NODE); // true
static final short NOTATION_NODE
The node is a Notation.
final var xml = """
<!DOCTYPE root [
<!NOTATION sample SYSTEM "aaa">
]>
<root/>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node notation = document.getDoctype().getNotations().item(0);
System.out.println(notation); // [sample: null]
System.out.println(Node.NOTATION_NODE); // 12
System.out.println(notation.getNodeType() == Node.NOTATION_NODE); // true
static final short PROCESSING_INSTRUCTION_NODE
The node is a ProcessingInstruction.
final var xml = """
<root><?target content?></root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node pi = document.getDocumentElement().getFirstChild();
System.out.println(pi); // [target: content]
System.out.println(Node.PROCESSING_INSTRUCTION_NODE); // 7
System.out.println(pi.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE); // true
static final short TEXT_NODE
The node is a Text node.
final var xml = """
<root>abcd</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node text = document.getDocumentElement().getFirstChild();
System.out.println(text); // [#text: abcd]
System.out.println(Node.TEXT_NODE); // 3
System.out.println(text.getNodeType() == Node.TEXT_NODE); // true
Methods
Node appendChild (Node newChild)
Adds the node newChild to the end of the list of children of this node.
final var xml = """
<root/>
""";
final var document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
final Node child = document.createElement("child");
System.out.println(child); // [child: null]
final Node ret = root.appendChild(child);
System.out.println(ret); // [child: null]
//<root>
// <child/>
//</root>
printDocument(document);
Node cloneNode (boolean deep)
Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes.
final var xml = """
<root><a><b>xyz</b></a></root>
""";
final var document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// <a>
// <b>xyz</b>
// </a>
//</root>
printDocument(document);
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
final Node childA = document.getElementsByTagName("a").item(0);
System.out.println(childA); // [a: null]
final Node clonedDeepNode = childA.cloneNode(true);
final Node clonedNode = childA.cloneNode(false);
root.appendChild(clonedDeepNode);
root.appendChild(clonedNode);
//<root>
// <a>
// <b>xyz</b>
// </a>
// <a>
// <b>xyz</b>
// </a>
// <a/>
//</root>
printDocument(document);
short compareDocumentPosition (Node other)
Compares the reference node, i.e. the node on which this method is being called, with a node, i.e. the one passed as a parameter, with regard to their position in the document and according to the document order.
final var xml = """
<root>
<child/>
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
final Node child = document.getElementsByTagName("child").item(0);
System.out.println(child); // [child: null]
final short type = root.compareDocumentPosition(child);
System.out.printf("%#x%n", type); // 0x14
System.out.printf("%#x%n", Node.DOCUMENT_POSITION_CONTAINED_BY); // 0x10
System.out.printf("%#x%n", type & Node.DOCUMENT_POSITION_CONTAINED_BY); // 0x10
System.out.printf("%#x%n", Node.DOCUMENT_POSITION_FOLLOWING); // 0x4
System.out.printf("%#x%n", type & Node.DOCUMENT_POSITION_FOLLOWING); // 0x4
NamedNodeMap getAttributes ()
A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.
final var xml = """
<root>
<child aa="AA" bb="BB" />
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node child = document.getElementsByTagName("child").item(0);
System.out.println(child); // [child: null]
final var attributes = child.getAttributes();
System.out.println("-- attributes --");
for (int i = 0; i < attributes.getLength(); i++) {
final var attr = attributes.item(i);
System.out.println(attr);
}
// Result
// ↓
//-- attributes --
//aa="AA"
//bb="BB"
String getBaseURI ()
The absolute base URI of this node or null if the implementation wasn't able to obtain an absolute URI.
final var path = Path.of("R:", "java-work", "a.xml");
System.out.println(path); // R:\java-work\a.xml
Files.writeString(path, """
<root>
<child/>
</root>
""");
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(path.toFile());
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
System.out.println(root.getBaseURI()); // file:/R:/java-work/a.xml
NodeList getChildNodes ()
A NodeList that contains all children of this node.
final var xml = """
<root><child-a/><child-b/></root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var nodes = root.getChildNodes();
System.out.println(nodes.getLength()); // 2
System.out.println("-- nodes --");
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// Result
// ↓
//-- nodes --
//[child-a: null]
//[child-b: null]
final var xml = """
<root>
<child-a/>
<child-b/>
<!-- comment! -->
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
final var nodes = root.getChildNodes();
System.out.println(nodes.getLength()); // 7
System.out.println("-- nodes --");
for (int i = 0; i < nodes.getLength(); i++) {
final var node = nodes.item(i);
if (node instanceof Text text) {
// For readability, line separators are converted to "\n" and spaces to "*".
final var data = text.getData().replace("\n", "\\n").replace(" ", "*");
System.out.println("[#text: " + data + "]");
} else {
System.out.println(node);
}
}
// Result
// ↓
//-- nodes --
//[#text: \n****]
//[child-a: null]
//[#text: \n****]
//[child-b: null]
//[#text: \n****]
//[#comment: comment! ]
//[#text: \n]
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 .
final var xml = """
<root>abcd</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
System.out.println(root.isSupported("XML", "3.0")); // true
System.out.println(root.getFeature("XML", "3.0")); // [root: null]
System.out.println(root.isSupported("XML", "4.0")); // false
System.out.println(root.getFeature("XML", "4.0")); // null
Node getFirstChild ()
The first child of this node.
final var xml = """
<root><child-a/><child-b/><child-c/></root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// <child-a/>
// <child-b/>
// <child-c/>
//</root>
printDocument(document);
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
final Node firstChild = root.getFirstChild();
System.out.println(firstChild); // [child-a: null]
final Node lastChild = root.getLastChild();
System.out.println(lastChild); // [child-c: null]
Node getLastChild ()
The last child of this node.
final var xml = """
<root><child-a/><child-b/><child-c/></root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// <child-a/>
// <child-b/>
// <child-c/>
//</root>
printDocument(document);
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
final Node firstChild = root.getFirstChild();
System.out.println(firstChild); // [child-a: null]
final Node lastChild = root.getLastChild();
System.out.println(lastChild); // [child-c: null]
String getLocalName ()
Returns the local part of the qualified name of this node.
final var xml = """
<ns:root xmlns:ns="sample">
<ns:child/>
</ns:root>
""";
final var builder = DocumentBuilderFactory.newNSInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node child = document.getElementsByTagName("ns:child").item(0);
System.out.println(child); // [ns:child: null]
System.out.println(child.getPrefix()); // ns
System.out.println(child.getLocalName()); // child
System.out.println(child.getNamespaceURI()); // sample
String getNamespaceURI ()
The namespace URI of this node, or null if it is unspecified (see ).
final var xml = """
<ns:root xmlns:ns="sample">
<ns:child/>
</ns:root>
""";
final var builder = DocumentBuilderFactory.newNSInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node child = document.getElementsByTagName("ns:child").item(0);
System.out.println(child); // [ns:child: null]
System.out.println(child.getPrefix()); // ns
System.out.println(child.getLocalName()); // child
System.out.println(child.getNamespaceURI()); // sample
Node getNextSibling ()
The node immediately following this node.
final var xml = """
<root><child-a/><child-b/></root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// <child-a/>
// <child-b/>
//</root>
printDocument(document);
final Node childA = document.getDocumentElement().getFirstChild();
System.out.println(childA); // [child-a: null]
final Node childB = childA.getNextSibling();
System.out.println(childB); // [child-b: null]
final Node childC = childB.getNextSibling();
System.out.println(childC); // null
String getNodeName ()
The name of this node, depending on its type; see the table above.
final var xml = """
<root>
<child aa="AA">abcd</child>
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
// Element
final Node child = document.getElementsByTagName("child").item(0);
System.out.println(child.getNodeType() == Node.ELEMENT_NODE); // true
System.out.println(child.getNodeName()); // child
System.out.println(child.getNodeValue()); // null
// Attr
final Node attr = child.getAttributes().item(0);
System.out.println(attr.getNodeType() == Node.ATTRIBUTE_NODE); // true
System.out.println(attr.getNodeName()); // aa
System.out.println(attr.getNodeValue()); // AA
// Text
final Node text = child.getFirstChild();
System.out.println(text.getNodeType() == Node.TEXT_NODE); // true
System.out.println(text.getNodeName()); // #text
System.out.println(text.getNodeValue()); // abcd
short getNodeType ()
A code representing the type of the underlying object, as defined above.
final var xml = """
<root>
<child aa="AA">abcd</child>
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
// Element
final Node child = document.getElementsByTagName("child").item(0);
System.out.println(child.getNodeType() == Node.ELEMENT_NODE); // true
System.out.println(child.getNodeName()); // child
System.out.println(child.getNodeValue()); // null
// Attr
final Node attr = child.getAttributes().item(0);
System.out.println(attr.getNodeType() == Node.ATTRIBUTE_NODE); // true
System.out.println(attr.getNodeName()); // aa
System.out.println(attr.getNodeValue()); // AA
// Text
final Node text = child.getFirstChild();
System.out.println(text.getNodeType() == Node.TEXT_NODE); // true
System.out.println(text.getNodeName()); // #text
System.out.println(text.getNodeValue()); // abcd
String getNodeValue ()
The value of this node, depending on its type; see the table above.
final var xml = """
<root>
<child aa="AA">abcd</child>
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
// Element
final Node child = document.getElementsByTagName("child").item(0);
System.out.println(child.getNodeType() == Node.ELEMENT_NODE); // true
System.out.println(child.getNodeName()); // child
System.out.println(child.getNodeValue()); // null
// Attr
final Node attr = child.getAttributes().item(0);
System.out.println(attr.getNodeType() == Node.ATTRIBUTE_NODE); // true
System.out.println(attr.getNodeName()); // aa
System.out.println(attr.getNodeValue()); // AA
// Text
final Node text = child.getFirstChild();
System.out.println(text.getNodeType() == Node.TEXT_NODE); // true
System.out.println(text.getNodeName()); // #text
System.out.println(text.getNodeValue()); // abcd
Document getOwnerDocument ()
The Document object associated with this node.
final var xml = """
<root>
<child/>
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node child = document.getElementsByTagName("child").item(0);
System.out.println(child); // [child: null]
final var ownerDocument = child.getOwnerDocument();
System.out.println(document.isSameNode(ownerDocument)); // true
Node getParentNode ()
The parent of this node.
final var xml = """
<root>
<child>
<child-child/>
</child>
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node childChild = document.getElementsByTagName("child-child").item(0);
System.out.println(childChild); // [child-child: null]
final Node child = childChild.getParentNode();
System.out.println(child); // [child: null]
final Node root = child.getParentNode();
System.out.println(root); // [root: null]
String getPrefix ()
The namespace prefix of this node, or null if it is unspecified.
final var xml = """
<ns:root xmlns:ns="sample">
<ns:child/>
</ns:root>
""";
final var builder = DocumentBuilderFactory.newNSInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node child = document.getElementsByTagName("ns:child").item(0);
System.out.println(child); // [ns:child: null]
System.out.println(child.getPrefix()); // ns
System.out.println(child.getLocalName()); // child
System.out.println(child.getNamespaceURI()); // sample
Node getPreviousSibling ()
The node immediately preceding this node.
final var xml = """
<root><child-y/><child-z/></root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// <child-y/>
// <child-z/>
//</root>
printDocument(document);
final Node childZ = document.getDocumentElement().getLastChild();
System.out.println(childZ); // [child-z: null]
final Node childY = childZ.getPreviousSibling();
System.out.println(childY); // [child-y: null]
final Node childX = childY.getPreviousSibling();
System.out.println(childX); // null
String getTextContent ()
This attribute returns the text content of this node and its descendants.
final var xml = """
<root>aaa<child>XYZ</child>bbb</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
System.out.println(root.getTextContent()); // aaaXYZbbb
final Node child = document.getElementsByTagName("child").item(0);
System.out.println(child); // [child: null]
System.out.println(child.getTextContent()); // XYZ
Object getUserData (String key)
Retrieves the object associated to a key on a this node.
final var xml = """
<root>
<child-a/>
<child-b/>
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
final Node childB = document.getElementsByTagName("child-b").item(0);
System.out.println(childB); // [child-b: null]
final var handler = new UserDataHandler() {
@Override
public void handle(short operation, String key, Object data, Node src, Node dst) {
System.out.println("-- UserDataHandler.handle --");
System.out.println(
"operation = " + operation + " : key = " + key +
" : data = " + data + " : src = " + src + " : dst = " + dst);
}
};
final var retA = childA.setUserData("a-key", "aaa", handler);
System.out.println(retA); // null
final var retB = childB.setUserData("b-key", "bbb", handler);
System.out.println(retB); // null
System.out.println(childA.getUserData("a-key")); // aaa
System.out.println(childA.getUserData("b-key")); // null
System.out.println(childB.getUserData("a-key")); // null
System.out.println(childB.getUserData("b-key")); // bbb
final var clonedNode = childA.cloneNode(false);
// Result
// ↓
//-- UserDataHandler.handle --
//operation = 1 : key = a-key : data = aaa : src = [child-a: null] : dst = [child-a: null]
final var renamedNode = document.renameNode(childB, null, "child-z");
// Result
// ↓
//-- UserDataHandler.handle --
//operation = 4 : key = b-key : data = bbb : src = [child-z: null] : dst = null
boolean hasAttributes ()
Returns whether this node (if it is an element) has any attributes.
final var xml = """
<root>
<child-a/>
<child-b aa="AA" />
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
System.out.println(childA.hasAttributes()); // false
final Node childB = document.getElementsByTagName("child-b").item(0);
System.out.println(childB); // [child-b: null]
System.out.println(childB.hasAttributes()); // true
boolean hasChildNodes ()
Returns whether this node has any children.
final var xml = """
<root><child/></root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
System.out.println(root.hasChildNodes()); // true
System.out.println(root.getChildNodes().getLength()); // 1
final Node child = document.getElementsByTagName("child").item(0);
System.out.println(child); // [child: null]
System.out.println(child.hasChildNodes()); // false
System.out.println(child.getChildNodes().getLength()); // 0
Node insertBefore (Node newChild, Node refChild)
Inserts the node newChild before the existing child node refChild.
final var xml = """
<root><child-z/></root>
""";
final var document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// <child-z/>
//</root>
printDocument(document);
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
final Node childZ = document.getElementsByTagName("child-z").item(0);
System.out.println(childZ); // [child-z: null]
final Node childX = document.createElement("child-x");
final Node ret1 = root.insertBefore(childX, childZ);
System.out.println(ret1); // [child-x: null]
final Node childY = document.createElement("child-y");
final Node ret2 = root.insertBefore(childY, childZ);
System.out.println(ret2); // [child-y: null]
//<root>
// <child-x/>
// <child-y/>
// <child-z/>
//</root>
printDocument(document);
boolean isDefaultNamespace (String namespaceURI)
This method checks if the specified namespaceURI is the default namespace or not.
final var xml = """
<root xmlns="sample1" xmlns:ns="sample2">
<child-a/>
<ns:child-b/>
</root>
""";
final var builder = DocumentBuilderFactory.newNSInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
System.out.println(childA.getNamespaceURI()); // sample1
System.out.println(childA.isDefaultNamespace("sample1")); // true
System.out.println(childA.isDefaultNamespace("sample2")); // false
final Node childB = document.getElementsByTagName("ns:child-b").item(0);
System.out.println(childB); // [ns:child-b: null]
System.out.println(childB.getNamespaceURI()); // sample2
System.out.println(childB.isDefaultNamespace("sample1")); // true
System.out.println(childB.isDefaultNamespace("sample2")); // false
boolean isEqualNode (Node arg)
Tests whether two nodes are equal.
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.newDocument();
final Node nodeA1 = document.createElement("node-a");
final Node nodeA2 = document.createElement("node-a");
final Node nodeB = document.createElement("node-b");
System.out.println(nodeA1.isEqualNode(nodeA1)); // true
System.out.println(nodeA1.isEqualNode(nodeA2)); // true
System.out.println(nodeA1.isEqualNode(nodeB)); // false
System.out.println(nodeA1.isSameNode(nodeA1)); // true
System.out.println(nodeA1.isSameNode(nodeA2)); // false
System.out.println(nodeA1.isSameNode(nodeB)); // false
boolean isSameNode (Node other)
Returns whether this node is the same node as the given one.
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.newDocument();
final Node nodeA1 = document.createElement("node-a");
final Node nodeA2 = document.createElement("node-a");
final Node nodeB = document.createElement("node-b");
System.out.println(nodeA1.isEqualNode(nodeA1)); // true
System.out.println(nodeA1.isEqualNode(nodeA2)); // true
System.out.println(nodeA1.isEqualNode(nodeB)); // false
System.out.println(nodeA1.isSameNode(nodeA1)); // true
System.out.println(nodeA1.isSameNode(nodeA2)); // false
System.out.println(nodeA1.isSameNode(nodeB)); // false
boolean isSupported (String feature, String version)
Tests whether the DOM implementation implements a specific feature and that feature is supported by this node, as specified in .
final var xml = """
<root>abcd</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
System.out.println(root.isSupported("XML", "3.0")); // true
System.out.println(root.getFeature("XML", "3.0")); // [root: null]
System.out.println(root.isSupported("XML", "4.0")); // false
System.out.println(root.getFeature("XML", "4.0")); // null
String lookupNamespaceURI (String prefix)
Look up the namespace URI associated to the given prefix, starting from this node.
final var xml = """
<root xmlns:ns1="sample1">
<ns1:child-a>
<ns2:child-b xmlns:ns2="sample2"/>
</ns1:child-a>
</root>
""";
final var builder = DocumentBuilderFactory.newNSInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node childA = document.getElementsByTagName("ns1:child-a").item(0);
System.out.println(childA); // [ns1:child-a: null]
System.out.println(childA.lookupNamespaceURI("ns1")); // sample1
System.out.println(childA.lookupNamespaceURI("ns2")); // null
final Node childB = document.getElementsByTagName("ns2:child-b").item(0);
System.out.println(childB); // [ns2:child-b: null]
System.out.println(childB.lookupNamespaceURI("ns1")); // sample1
System.out.println(childB.lookupNamespaceURI("ns2")); // sample2
String lookupPrefix (String namespaceURI)
Look up the prefix associated to the given namespace URI, starting from this node.
final var xml = """
<root xmlns:ns1="sample1">
<ns1:child-a>
<ns2:child-b xmlns:ns2="sample2"/>
</ns1:child-a>
</root>
""";
final var builder = DocumentBuilderFactory.newNSInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node childA = document.getElementsByTagName("ns1:child-a").item(0);
System.out.println(childA); // [ns1:child-a: null]
System.out.println(childA.lookupPrefix("sample1")); // ns1
System.out.println(childA.lookupPrefix("sample2")); // null
final Node childB = document.getElementsByTagName("ns2:child-b").item(0);
System.out.println(childB); // [ns2:child-b: null]
System.out.println(childB.lookupPrefix("sample1")); // ns1
System.out.println(childB.lookupPrefix("sample2")); // ns2
void normalize ()
Puts all Text nodes in the full depth of the sub-tree underneath this Node, including attribute nodes, into a "normal" form where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are neither adjacent Text nodes nor empty Text nodes.
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.newDocument();
final Node root = document.createElement("root");
// Intentionally creates Text nodes by splitting it.
root.appendChild(document.createTextNode("aaa"));
root.appendChild(document.createTextNode("bbb"));
root.appendChild(document.createElement("child"));
root.appendChild(document.createTextNode(""));
{
final var nodes = root.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// Result
// ↓
//[#text: aaa]
//[#text: bbb]
//[child: null]
//[#text: ]
}
// Adjacent text nodes are combined. Empty Text nodes are deleted.
root.normalize();
{
final var nodes = root.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// Result
// ↓
//[#text: aaabbb]
//[child: null]
}
Node removeChild (Node oldChild)
Removes the child node indicated by oldChild from the list of children, and returns it.
final var xml = """
<root><child>abcd</child></root>
""";
final var document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// <child>abcd</child>
//</root>
printDocument(document);
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
final Node child = document.getElementsByTagName("child").item(0);
System.out.println(child); // [child: null]
final Node ret = root.removeChild(child);
System.out.println(ret); // [child: null]
//<root/>
printDocument(document);
Node replaceChild (Node newChild, Node oldChild)
Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node.
final var xml = """
<root><child-a>abcd</child-a></root>
""";
final var document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// <child-a>abcd</child-a>
//</root>
printDocument(document);
final Node root = document.getDocumentElement();
System.out.println(root); // [root: null]
final Node childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
final Node childB = document.createElement("child-b");
childB.appendChild(document.createTextNode("XYZ"));
final Node ret = root.replaceChild(childB, childA);
System.out.println(ret); // [child-a: null]
//<root>
// <child-b>XYZ</child-b>
//</root>
printDocument(document);
void setNodeValue (String nodeValue)
The value of this node, depending on its type; see the table above.
final var xml = """
<root><child aa="AA"/></root>
""";
final var document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// <child aa="AA"/>
//</root>
printDocument(document);
final Node child = document.getElementsByTagName("child").item(0);
System.out.println(child); // [child: null]
final Node attr = child.getAttributes().item(0);
attr.setNodeValue("XYZ");
//<root>
// <child aa="XYZ"/>
//</root>
printDocument(document);
void setPrefix (String prefix)
The namespace prefix of this node, or null if it is unspecified.
final var xml = """
<ns:root xmlns:ns="sample"><ns:child/></ns:root>
""";
final var document = DocumentBuilderFactory.newNSInstance()
.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
//<ns:root xmlns:ns="sample">
// <ns:child/>
//</ns:root>
printDocument(document);
final Node child = document.getElementsByTagName("ns:child").item(0);
System.out.println(child); // [ns:child: null]
child.setPrefix("xx");
//<ns:root xmlns:ns="sample">
// <xx:child xmlns:xx="sample"/>
//</ns:root>
printDocument(document);
void setTextContent (String textContent)
This attribute returns the text content of this node and its descendants.
final var xml = """
<root><child/></root>
""";
final var document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
//<root>
// <child/>
//</root>
printDocument(document);
final Node child = document.getElementsByTagName("child").item(0);
System.out.println(child); // [child: null]
child.setTextContent("abcd");
//<root>
// <child>abcd</child>
//</root>
printDocument(document);
Object setUserData (String key, Object data, UserDataHandler handler)
Associate an object to a key on this node.
final var xml = """
<root>
<child-a/>
<child-b/>
</root>
""";
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final Node childA = document.getElementsByTagName("child-a").item(0);
System.out.println(childA); // [child-a: null]
final Node childB = document.getElementsByTagName("child-b").item(0);
System.out.println(childB); // [child-b: null]
final var handler = new UserDataHandler() {
@Override
public void handle(short operation, String key, Object data, Node src, Node dst) {
System.out.println("-- UserDataHandler.handle --");
System.out.println(
"operation = " + operation + " : key = " + key +
" : data = " + data + " : src = " + src + " : dst = " + dst);
}
};
final var retA = childA.setUserData("a-key", "aaa", handler);
System.out.println(retA); // null
final var retB = childB.setUserData("b-key", "bbb", handler);
System.out.println(retB); // null
System.out.println(childA.getUserData("a-key")); // aaa
System.out.println(childA.getUserData("b-key")); // null
System.out.println(childB.getUserData("a-key")); // null
System.out.println(childB.getUserData("b-key")); // bbb
final var clonedNode = childA.cloneNode(false);
// Result
// ↓
//-- UserDataHandler.handle --
//operation = 1 : key = a-key : data = aaa : src = [child-a: null] : dst = [child-a: null]
final var renamedNode = document.renameNode(childB, null, "child-z");
// Result
// ↓
//-- UserDataHandler.handle --
//operation = 4 : key = b-key : data = bbb : src = [child-z: null] : dst = null