forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaXmlDomReadEx.java
More file actions
59 lines (40 loc) · 1.84 KB
/
JavaXmlDomReadEx.java
File metadata and controls
59 lines (40 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.zetcode;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
public class JavaXmlDomReadEx {
public static void main(String argv[]) throws SAXException,
IOException, ParserConfigurationException {
var xmlFile = new File("src/main/resources/users.xml");
var factory = DocumentBuilderFactory.newInstance();
var dBuilder = factory.newDocumentBuilder();
var doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
// NodeList
var nList = doc.getElementsByTagName("user");
for (int i = 0; i < nList.getLength(); i++) {
// Node
var nNode = nList.item(i);
System.out.println("\nCurrent Element: " + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
var elem = (Element) nNode;
var uid = elem.getAttribute("id");
var node1 = elem.getElementsByTagName("firstname").item(0);
var firstName = node1.getTextContent();
var node2 = elem.getElementsByTagName("lastname").item(0);
var lastName = node2.getTextContent();
var node3 = elem.getElementsByTagName("occupation").item(0);
var occupation = node3.getTextContent();
System.out.printf("User id: %s%n", uid);
System.out.printf("First name: %s%n", firstName);
System.out.printf("Last name: %s%n", lastName);
System.out.printf("Occupation: %s%n", occupation);
}
}
}
}