-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOMExample.java
More file actions
60 lines (48 loc) · 1.76 KB
/
Copy pathDOMExample.java
File metadata and controls
60 lines (48 loc) · 1.76 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
60
package com.dom;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DOMExample {
public static void main(String[] args) {
List<Student> listStudents = DOMExample.readListStudents();
for(Student student : listStudents) {
System.out.println(student.toString());
}
}
public static List<Student> readListStudents() {
List<Student> listStudents = new ArrayList<>();
Student student = null;
try {
// /Java-DOM/input.xml
File inputFile = new File("input.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
System.out.println("Phan tu goc: " + doc.getDocumentElement().getNodeName());
NodeList nodeListStudent = doc.getElementsByTagName("student");
for(int i = 0; i < nodeListStudent.getLength(); i++) {
student = new Student();
Node nNode = nodeListStudent.item(i);
if(nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
student.setId(eElement.getAttribute("id"));
student.setFirstName(eElement.getElementsByTagName("firstname").item(0).getTextContent());
student.setLastName(eElement.getElementsByTagName("lastname").item(0).getTextContent());
student.setMarks(eElement.getElementsByTagName("marks").item(0).getTextContent());
}
listStudents.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}
return listStudents;
}
}