-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLToCSV.java
188 lines (155 loc) · 7.67 KB
/
XMLToCSV.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
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
178
179
180
181
182
183
184
185
186
187
188
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.util.*;
// This Java program reads the XML and converts it into CSV files, adhering to the relational schema we designed.
// The CSV files will use a tab delimiter ("\t") and end-of-line character "\n".
// It's using DOM parsing
public class XMLToCSV {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Please provide the input XML file path and the output directory path as command line arguments.");
return;
}
String inputFilePath = args[0];
String outputDirPath = args[1];
try {
System.out.println("Starting XML to CSV conversion...");
File inputFile = new File(inputFilePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
parseItems(doc, outputDirPath);
System.out.println("Items.csv created.");
parseItemCategory(doc, outputDirPath);
System.out.println("ItemCategory.csv created.");
parseBids(doc, outputDirPath);
System.out.println("Bids.csv created.");
parseBidder(doc, outputDirPath);
System.out.println("Bidder.csv created.");
parseSeller(doc, outputDirPath);
System.out.println("Seller.csv created.");
System.out.println("XML to CSV conversion completed successfully.");
} catch (Exception e) {
e.printStackTrace();
System.out.println("An error occurred during the XML to CSV conversion.");
}
}
private static void parseItems(Document doc, String outputDirPath) throws IOException {
NodeList nodeList = doc.getElementsByTagName("Item");
File file = new File(outputDirPath + "/Items.csv");
boolean isNewFile = !file.exists();
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
if (isNewFile) {
bw.write("ItemID\tName\tCurrently\tBuy_Price\tFirst_Bid\tNumber_of_Bids\tLocation\tCountry\tStarted\tEnds\tDescription\n");
}
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = (Element) nodeList.item(i);
String itemID = element.getAttribute("ItemID");
String name = getTagValue("Name", element);
String currently = getTagValue("Currently", element);
String buyPrice = getTagValue("Buy_Price", element);
String firstBid = getTagValue("First_Bid", element);
String numberOfBids = getTagValue("Number_of_Bids", element);
String location = getTagValue("Location", element);
String country = getTagValue("Country", element);
String started = getTagValue("Started", element);
String ends = getTagValue("Ends", element);
String description = getTagValue("Description", element);
bw.write(String.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
itemID, name, currently, buyPrice, firstBid, numberOfBids, location, country, started, ends, description));
}
bw.close();
}
private static void parseItemCategory(Document doc, String outputDirPath) throws IOException {
NodeList nodeList = doc.getElementsByTagName("Item");
File file = new File(outputDirPath + "/ItemCategory.csv");
boolean isNewFile = !file.exists();
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
if (isNewFile) {
bw.write("ItemID\tCategory\n");
}
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = (Element) nodeList.item(i);
String itemID = element.getAttribute("ItemID");
NodeList categories = element.getElementsByTagName("Category");
for (int j = 0; j < categories.getLength(); j++) {
String category = categories.item(j).getTextContent();
bw.write(String.format("%s\t%s\n", itemID, category));
}
}
bw.close();
}
private static void parseBids(Document doc, String outputDirPath) throws IOException {
NodeList nodeList = doc.getElementsByTagName("Bid");
File file = new File(outputDirPath + "/Bids.csv");
boolean isNewFile = !file.exists();
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
if (isNewFile) {
bw.write("BidderID\tTime\tAmount\n");
}
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = (Element) nodeList.item(i);
String bidderID = ((Element)element.getElementsByTagName("Bidder").item(0)).getAttribute("UserID");
String time = getTagValue("Time", element);
String amount = getTagValue("Amount", element);
bw.write(String.format("%s\t%s\t%s\n", bidderID, time, amount));
}
bw.close();
}
private static void parseBidder(Document doc, String outputDirPath) throws IOException {
NodeList nodeList = doc.getElementsByTagName("Bidder");
File file = new File(outputDirPath + "/Bidder.csv");
boolean isNewFile = !file.exists();
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
if (isNewFile) {
bw.write("UserID\tRating\tLocation\tCountry\n");
}
Set<String> bidders = new HashSet<>();
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = (Element) nodeList.item(i);
String userID = element.getAttribute("UserID");
if (!bidders.contains(userID)) {
bidders.add(userID);
String rating = element.getAttribute("Rating");
String location = getTagValue("Location", element);
String country = getTagValue("Country", element);
bw.write(String.format("%s\t%s\t%s\t%s\n", userID, rating, location, country));
}
}
bw.close();
}
private static void parseSeller(Document doc, String outputDirPath) throws IOException {
NodeList nodeList = doc.getElementsByTagName("Seller");
File file = new File(outputDirPath + "/Seller.csv");
boolean isNewFile = !file.exists();
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
if (isNewFile) {
bw.write("UserID\tRating\n");
}
Set<String> sellers = new HashSet<>();
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = (Element) nodeList.item(i);
String userID = element.getAttribute("UserID");
if (!sellers.contains(userID)) {
sellers.add(userID);
String rating = element.getAttribute("Rating");
bw.write(String.format("%s\t%s\n", userID, rating));
}
}
bw.close();
}
private static String getTagValue(String tag, Element element) {
NodeList nodeList = element.getElementsByTagName(tag);
if (nodeList.getLength() > 0) {
return nodeList.item(0).getTextContent();
}
return "";
}
}