Java : Node (XML) - API使用例
Node (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
API仕様書のおともにどうぞ。
概要
Nodeインタフェースは、Document Object Model(DOM)のもっとも基本となるデータ型です。
より具体的なElementやTextといったデータ型は、すべてNodeを実装します。
Nodeは1つの親と複数の子を持ちます。
全体としてツリー構造になります。
関連記事:XML (DOM) の基本操作
本記事のコード例では、利便性のために以下の printDocument メソッドを使います。
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);
フィールド
static final short ATTRIBUTE_NODE
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
メソッド
Node appendChild (Node newChild)
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)
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)
結果は、フィールド定数の DOCUMENT_POSITION_~ になります。
DOCUMENT_POSITION_~ については、各フィールド定数のAPI使用例をご確認ください。
結果は1つとは限らず、複数になることもあります。
つまり DOCUMENT_POSITION_~ は論理演算で確認する必要があります。
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 ()
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);
}
// 結果
// ↓
//-- attributes --
//aa="AA"
//bb="BB"
String getBaseURI ()
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 ()
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));
}
// 結果
// ↓
//-- 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) {
// 見やすさのために、改行は \n、スペースは * に変換します。
final var data = text.getData().replace("\n", "\\n").replace(" ", "*");
System.out.println("[#text: " + data + "]");
} else {
System.out.println(node);
}
}
// 結果
// ↓
//-- nodes --
//[#text: \n****]
//[child-a: null]
//[#text: \n****]
//[child-b: null]
//[#text: \n****]
//[#comment: comment! ]
//[#text: \n]
Object getFeature (String feature, String version)
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 ()
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 ()
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 ()
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 ()
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 ()
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 ()
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 ()
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 ()
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 ()
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 ()
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 ()
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 ()
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 ()
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)
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);
// 結果
// ↓
//-- 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");
// 結果
// ↓
//-- UserDataHandler.handle --
//operation = 4 : key = b-key : data = bbb : src = [child-z: null] : dst = null
boolean hasAttributes ()
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 ()
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)
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)
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)
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)
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)
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)
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)
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 ()
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.newDocument();
final Node root = document.createElement("root");
// 意図的にTextノードを不必要に分割して作成します。
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));
}
// 結果
// ↓
//[#text: aaa]
//[#text: bbb]
//[child: null]
//[#text: ]
}
// 結合可能なTextは結合されます。空のTextは削除されます。
root.normalize();
{
final var nodes = root.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i));
}
// 結果
// ↓
//[#text: aaabbb]
//[child: null]
}
Node removeChild (Node oldChild)
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)
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)
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)
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)
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)
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);
// 結果
// ↓
//-- 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");
// 結果
// ↓
//-- UserDataHandler.handle --
//operation = 4 : key = b-key : data = bbb : src = [child-z: null] : dst = null