forked from davidjava1991/java17CompleteCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.java
More file actions
52 lines (46 loc) · 1.69 KB
/
Copy pathExample2.java
File metadata and controls
52 lines (46 loc) · 1.69 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
package com.david.class154;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Example2 {
public static void main(String[] args) {
File file = new File("./in/Customers.xml");
ArrayList<Customer> cList = new ArrayList<>();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("customer");
Customer c;
for(int i = 0;i<nList.getLength();i++) {
c = new Customer();
Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node;
c.id = Integer.parseInt(e.getElementsByTagName("id").item(0).getTextContent());
c.name = e.getElementsByTagName("name").item(0).getTextContent();
c.address = e.getElementsByTagName("address").item(0).getTextContent();
cList.add(c);
}
}
cList.forEach(e ->{
System.out.println("---------------");
System.out.println("id : "+e.id);
System.out.println("name : "+e.name);
System.out.println("address : "+e.address);
});
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
}