-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyContentHandler.java
89 lines (78 loc) · 2.18 KB
/
MyContentHandler.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
package eCANDY;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyContentHandler extends DefaultHandler implements ErrorHandler{
private Product product;
private ArrayList<Product> productList = new ArrayList<>();
private String content;
public ArrayList<Product> getProductList() {
return productList;
}
public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes attrs)
throws SAXException {
if (qualifiedName.equalsIgnoreCase("Item"))
product = new Product();
if (qualifiedName.equalsIgnoreCase(("Product")) && product == null)
product = new Product();
loopOverAttributes(attrs);
}
private void loopOverAttributes(Attributes attrs) {
for (int i = 0; i < attrs.getLength(); i++) {
switch (attrs.getQName(i).toLowerCase()) {
case "name":
product.setName(attrs.getValue(i));
break;
case "price":
try {
product.setPrice(NumberFormat.getInstance().parse(attrs.getValue(i)).doubleValue());
} catch (ParseException e) {
// System.err.println(e.getMessage());
}
break;
case "currency":
product.setCurrency(attrs.getValue(i));
break;
case "bestbefore":
case "date":
product.setBestBefore(attrs.getValue(i));
break;
default:
break;
}
}
}
public void endElement(String namespaceURI, String localName, String qualifiedName) throws SAXException {
content = content.trim();
switch (qualifiedName.toLowerCase()) {
case "product": {
if(product!=null)
productList.add(product);
product = null;
}
break;
case "id":product.setId(content);
case "name":
product.setName(content);
break;
case "currency":
product.setCurrency(content);
break;
case "price":
try {
product.setPrice(NumberFormat.getInstance().parse(content).doubleValue());
} catch (ParseException e) {
}
break;
default:
break;
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
content = new String(ch, start, length);
}
}