-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlParse.java
More file actions
129 lines (111 loc) · 3.81 KB
/
XmlParse.java
File metadata and controls
129 lines (111 loc) · 3.81 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
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
package ac.shenkar.Calc;
import java.net.*;
import java.text.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.io.*;
import org.apache.log4j.*;
public class XmlParse implements Runnable{
private NodeList name;
private NodeList unit;
private NodeList code;
private NodeList country;
private NodeList rate;
private NodeList change;
private DocumentBuilderFactory factory;
private DocumentBuilder builder;
private URL url;
private CurrencyTable currTable;
private Logger log;
private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public XmlParse() throws IOException, ParserConfigurationException, SAXException, TransformerException
{
log=Logger.getLogger("CurrencyLogs");
BasicConfigurator.configure();
try{
log.addAppender(new FileAppender(new SimpleLayout(),"log.txt"));
}
catch(IOException e){
e.printStackTrace();
}
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
url = new URL("http://www.boi.org.il/currency.xml");
if(url == null)
{
System.out.println("error!");
return;
}
init();
currTable=new CurrencyTable(name.getLength());
}
@Override
public void run() {
try {
init();
Date d=new Date();
log.info(sdf.format(d));
int length = name.getLength();
for (int i = 0; i < length; i++) {
currTable.addElement(i, new Currency(
name.item(i).getFirstChild().getNodeValue()
,Integer.parseInt(unit.item(i).getFirstChild().getNodeValue())
,code.item(i).getFirstChild().getNodeValue()
,country.item(i).getFirstChild().getNodeValue()
,Double.parseDouble(rate.item(i).getFirstChild().getNodeValue())
,Double.parseDouble(change.item(i).getFirstChild().getNodeValue())));
log.info(currTable.getRow(i).getRate());
}
} catch (IOException | ParserConfigurationException | SAXException | TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void init() throws IOException, ParserConfigurationException, SAXException, TransformerException {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy_MM_dd");
LocalDate localDate = LocalDate.now();
OutputStream fs=new FileOutputStream (".\\history\\"+dtf.format(localDate)+"_currency.xml");
InputStream in = null;
Document doc = null;
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
if (con!=null)
{
in = con.getInputStream();
doc = builder.parse(in);
//read xml URL to history currency xml files
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer xform = tfactory.newTransformer();
xform.transform(new DOMSource(doc), new StreamResult(fs));
fs=new FileOutputStream ("cur_currency.xml");
//read xml URL to update currency xml file
tfactory = TransformerFactory.newInstance();
xform = tfactory.newTransformer();
xform.transform(new DOMSource(doc), new StreamResult(fs));
con.disconnect();
}
//read xml data from local xml file
in=new FileInputStream("cur_currency.xml");
doc = builder.parse(in);
name = doc.getElementsByTagName("NAME");
unit = doc.getElementsByTagName("UNIT");
code = doc.getElementsByTagName("CURRENCYCODE");
country = doc.getElementsByTagName("COUNTRY");
rate = doc.getElementsByTagName("RATE");
change = doc.getElementsByTagName("CHANGE");
con.disconnect();
}
public CurrencyTable getCurrencyTable(){
return currTable;
}
}