-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDomParserExample.java
130 lines (100 loc) · 3.94 KB
/
DomParserExample.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DomParserExample {
List<Employee> employees = new ArrayList<>();
Document dom;
public void runExample() {
// parse the xml file and get the dom object
parseXmlFile();
// get each employee element and create a Employee object
parseDocument();
// iterate through the list and print the data
printData();
}
private void parseXmlFile() {
// get the factory
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
// using factory get an instance of document builder
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
// parse using builder to get DOM representation of the XML file
dom = documentBuilder.parse("employees.xml");
} catch (ParserConfigurationException | SAXException | IOException error) {
error.printStackTrace();
}
}
private void parseDocument() {
// get the document root Element
Element documentElement = dom.getDocumentElement();
// get a nodelist of employee Elements, parse each into Employee object
NodeList nodeList = documentElement.getElementsByTagName("Employee");
for (int i = 0; i < nodeList.getLength(); i++) {
// get the employee element
Element element = (Element) nodeList.item(i);
// get the Employee object
Employee employee = parseEmployee(element);
// add it to list
employees.add(employee);
}
}
/**
* It takes an employee Element, reads the values in, creates
* an Employee object for return
*/
private Employee parseEmployee(Element element) {
// for each <employee> element get text or int values of
// name ,id, age and name
String name = getTextValue(element, "Name");
int id = getIntValue(element, "Id");
int age = getIntValue(element, "Age");
String type = element.getAttribute("type");
// create a new Employee with the value read from the xml nodes
return new Employee(name, id, age, type);
}
/**
* It takes an XML element and the tag name, look for the tag and get
* the text content
* i.e for <Employee><Name>John</Name></Employee> xml snippet if
* the Element points to employee node and tagName is name it will return John
*/
private String getTextValue(Element element, String tagName) {
String textVal = null;
NodeList nodeList = element.getElementsByTagName(tagName);
if (nodeList.getLength() > 0) {
// here we expect only one <Name> would present in the <Employee>
textVal = nodeList.item(0).getFirstChild().getNodeValue();
}
return textVal;
}
/**
* Calls getTextValue and returns a int value
*/
private int getIntValue(Element ele, String tagName) {
// in production application you would catch the exception
return Integer.parseInt(getTextValue(ele, tagName));
}
/**
* Iterate through the list and print the
* content to console
*/
private void printData() {
System.out.println("Total parsed " + employees.size() + " employees");
for (Employee employee : employees) {
System.out.println("\t" + employee.toString());
}
}
public static void main(String[] args) {
// create an instance
DomParserExample domParserExample = new DomParserExample();
// call run example
domParserExample.runExample();
}
}