-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlparserselectinsert.js
177 lines (138 loc) · 4.74 KB
/
xmlparserselectinsert.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var https = require('https');
var parseString = require('xml2js').parseString;
var xml = '';
const converter = require('Json-2-csv');
const fs = require ('fs');
const csvjson = require('csvjson');
var csvfast = require('fast-csv');
function xmlToJson(url, callback) {
var req = https.get(url, function(res) {
var xml = '';
res.on('data', function(chunk) {
xml += chunk;
});
// res.on('error', function(e) {
// callback(e, null);
// });
// res.on('timeout', function(e) {
// callback(e, null);
// });
res.on('end', function() {
parseString(xml, function(err, result) {
callback(null, result);
});
});
});
}
var url = "https://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml"
const writeFile = async function (data, fileName) {
try {
fs.writeFileSync(fileName, data, 'utf-8');
}
catch (err) {
//sentry.captureException(err);
console.log(err);
}
return;
}
xmlToJson(url, function(err, data) {
if (err) {
return console.err(err);
}
var https = require('https');
var parseString = require('xml2js').parseString;
var xml = '';
const converter = require('Json-2-csv');
const fs = require ('fs');
function xmlToJson(url, callback) {
var req = https.get(url, function(res) {
var xml = '';
res.on('data', function(chunk) {
xml += chunk;
});
// res.on('error', function(e) {
// callback(e, null);
// });
// res.on('timeout', function(e) {
// callback(e, null);
// });
res.on('end', function() {
parseString(xml, function(err, result) {
callback(null, result);
});
});
});
}
var url = "https://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml"
xmlToJson(url, function(err, data) {
if (err) {
return console.err(err);
}
var value = data['soap:Envelope']['soap:Body'][0]['showResponse'][0]['showResult'][0]['diffgr:diffgram'][0]['NewDataSet'][0]['Table'];
//console.log(JSON.stringify(value, null, 2));
////////////////////////////////////
const csvData = csvjson.toCSV(JSON.stringify(value, null, 2), {
headers: 'key',
delimiter : '^'
});
writeFile(csvData, "test.csv");
///////*********** */
let counter = 0;
// let header = [];
// let data = [];
csvfast
let csvStream = csvjson.fromPath("test.csv", { headers: true })
.on("data", function(record){
csvStream.pause();
if(counter < 100)
{
let State=record.State;
let District=record.District;
let Market=record.Market;
let Commodity=record.Commodity;
let Variety=record.Variety;
let Arrival_Date=record.Arrival_Date;
let Min_x0020_Price=record.Min_x0020_Price;
let Max_x0020_Price=record.Max_x0020_Price;
let Modal_x0020_Price=record.Modal_x0020_Price;
// pool.query("INSERT INTO FL_insurance_sample(policyID, statecode, county, point_latitude, point_longitude, line, construction) \
// VALUES($1, $2, $3, $4, $5, $6, $7)", [policyID, statecode, county, point_latitude, point_longitude, line, construction], function(err){
// if(err)
// {
// console.log(err);
// }
// });
const pool = new Pool({
host: "aa11bulah17jx89.cdnwzu7jnz68.us-east-1.rds.amazonaws.com",
user: "vijay",
database: "ebdb",
password: "Farming002*",
port: 5432
});
pool.query =
("INSERT INTO commodity (State, District, Market, Commodity,Variety,Arrival_Date,Min_x0020_Price,Max_x0020_Price,Modal_x0020_Price) VALUES ($1, $2, $3, $4,$6,$7,$8,$9)",[State, District, Market, Commodity,Variety,Arrival_Date,Min_x0020_Price,Max_x0020_Price,Modal_x0020_Price],function(err){
if(err)
{
console.log(err);
}
});
++counter;
}
csvStream.resume();
}).on("end", function(){
console.log("Job is done!");
}).on("error", function(err){
console.log(err);
});
///////************* */
//////////////////////////////////////////
// var value = Table.diffgridTable1,
// length = TableArray.length;
// for(var i = 0; i < length; i++)
// {
// // You can access the customers array from here -
// console.log(TableArray[i]. diffgridTable1[0]); // [0] hardcoded because as you can see all the variables are in array at 0th position
// // similarly you can access other data
// }
});
});