8000 Major update · codepressed/Java-CSV2XML@87aaf9a · GitHub
[go: up one dir, main page]

Skip to content

Commit 87aaf9a

Browse files
author
A. Apesteguia
committed
Major update
1 parent f405b34 commit 87aaf9a

File tree

7 files changed

+120
-98
lines changed

7 files changed

+120
-98
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* Uses the traditional methodology to generate the xml (without a library), through
44
of nodes and tree structure.
55
* Can read ANY CSV File
6-
6+
* Args are: Input file, output file, element node name and optionally, if you add -s program will read the csv
7+
as semicolon-separated values instead of comma-separated values.
78
## Project test
89

910
### CSV Input File

examples/Grades.xml

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<root>
3-
<elements>
4-
<element>
3+
<Jhonnys>
4+
<Jhonny>
55
<Lastname>Alfalfa</Lastname>
66
<Firstname>Aloysius</Firstname>
77
<SSN>123-45-6789</SSN>
@@ -11,8 +11,8 @@
1111
<Test4>83.0</Test4>
1212
<Final>49.0</Final>
1313
<Grades>D-</Grades>
14-
</element>
15-
<element>
14+
</Jhonny>
15+
<Jhonny>
1616
<Lastname>Alfred</Lastname>
1717
<Firstname>University</Firstname>
1818
<SSN>123-12-1234</SSN>
@@ -22,8 +22,8 @@
2222
<Test4>97.0</Test4>
2323
<Final>48.0</Final>
2424
<Grades>D+</Grades>
25-
</element>
26-
<element>
25+
</Jhonny>
26+
<Jhonny>
2727
<Lastname>Gerty</Lastname>
2828
<Firstname>Gramma</Firstname>
2929
<SSN>567-89-0123</SSN>
@@ -33,8 +33,8 @@
3333
<Test4>40.0</Test4>
3434
<Final>44.0</Final>
3535
<Grades>C</Grades>
36-
</element>
37-
<element>
36+
</Jhonny>
37+
<Jhonny>
3838
<Lastname>Android</Lastname>
3939
<Firstname>Electric</Firstname>
4040
<SSN>087-65-4321</SSN>
@@ -44,8 +44,8 @@
4444
<Test4>45.0</Test4>
4545
<Final>47.0</Final>
4646
<Grades>B-</Grades>
47-
</element>
48-
<element>
47+
</Jhonny>
48+
<Jhonny>
4949
<Lastname>Bumpkin</Lastname>
5050
<Firstname>Fred</Firstname>
5151
<SSN>456-78-9012</SSN>
@@ -55,8 +55,8 @@
5555
<Test4>77.0</Test4>
5656
<Final>45.0</Final>
5757
<Grades>A-</Grades>
58-
</element>
59-
<element>
58+
</Jhonny>
59+
<Jhonny>
6060
<Lastname>Rubble</Lastname>
6161
<Firstname>Betty</Firstname>
6262
<SSN>234-56-7890</SSN>
@@ -66,8 +66,8 @@
6666
<Test4>90.0</Test4>
6767
<Final>46.0</Final>
6868
<Grades>C-</Grades>
69-
</element>
70-
<element>
69+
</Jhonny>
70+
<Jhonny>
7171
<Lastname>Noshow</Lastname>
7272
<Firstname>Cecil</Firstname>
7373
<SSN>345-67-8901</SSN>
@@ -77,8 +77,8 @@
7777
<Test4>4.0</Test4>
7878
<Final>43.0</Final>
7979
<Grades>F</Grades>
80-
</element>
81-
<element>
80+
</Jhonny>
81+
<Jhonny>
8282
<Lastname>Buff</Lastname>
8383
<Firstname>Bif</Firstname>
8484
<SSN>632-79-9939</SSN>
@@ -88,8 +88,8 @@
8888
<Test4>40.0</Test4>
8989
<Final>50.0</Final>
9090
<Grades>B+</Grades>
91-
</element>
92-
<element>
91+
</Jhonny>
92+
<Jhonny>
9393
<Lastname>Airpump</Lastname>
9494
<Firstname>Andrew</Firstname>
9595
<SSN>223-45-6789</SSN>
@@ -98,8 +98,8 @@
9898
<Test3>100.0</Test3>
9999
<Test4>83.0</Test4>
100100
<Final>A</Final>
101-
</element>
102-
<element>
101+
</Jhonny>
102+
<Jhonny>
103103
<Lastname>Backus</Lastname>
104104
<Firstname>Jim</Firstname>
105105
<SSN>143-12-1234</SSN>
@@ -109,8 +109,8 @@
109109
<Test4>96.0</Test4>
110110
<Final>97.0</Final>
111111
<Grades>A+</Grades>
112-
</element>
113-
<element>
112+
</Jhonny>
113+
<Jhonny>
114114
<Lastname>Carnivore</Lastname>
115115
<Firstname>Art</Firstname>
116116
<SSN>565-89-0123</SSN>
@@ -120,8 +120,8 @@
120120
<Test4>60.0</Test4>
121121
<Final>40.0</Final>
122122
<Grades>D+</Grades>
123-
</element>
124-
<element>
123+
</Jhonny>
124+
<Jhonny>
125125
<Lastname>Dandy</Lastname>
126126
<Firstname>Jim</Firstname>
127127
<SSN>087-75-4321</SSN>
@@ -131,8 +131,8 @@
131131
<Test4>36.0</Test4>
132132
<Final>45.0</Final>
133133
<Grades>C+</Grades>
134-
</element>
135-
<element>
134+
</Jhonny>
135+
<Jhonny>
136136
<Lastname>Elephant</Lastname>
137137
<Firstname>Ima</Firstname>
138138
<SSN>456-71-9012</SSN>
@@ -142,8 +142,8 @@
142142
<Test4>88.0</Test4>
143143
<Final>77.0</Final>
144144
<Grades>B-</Grades>
145-
</element>
146-
<element>
145+
</Jhonny>
146+
<Jhonny>
147147
<Lastname>Franklin</Lastname>
148148
<Firstname>Benny</Firstname>
149149
<SSN>234-56-2890</SSN>
@@ -153,8 +153,8 @@
153153
<Test4>80.0</Test4>
154154
<Final>90.0</Final>
155155
<Grades>B-</Grades>
156-
</element>
157-
<element>
156+
</Jhonny>
157+
<Jhonny>
158158
<Lastname>George</Lastname>
159159
<Firstname>Boy</Firstname>
160160
<SSN>345-67-3901</SSN>
@@ -164,8 +164,8 @@
164164
<Test4>-1.0</Test4>
165165
<Final>4.0</Final>
166166
<Grades>B</Grades>
167-
</element>
168-
<element>
167+
</Jhonny>
168+
<Jhonny>
169169
<Lastname>Heffalump</Lastname>
170170
<Firstname>Harvey</Firstname>
171171
<SSN>632-79-9439</SSN>
@@ -175,6 +175,6 @@
175175
<Test4>30.0</Test4>
176176
<Final>40.0</Final>
177177
<Grades>C</Grades>
178-
</element>
179-
</elements>
178+
</Jhonny>
179+
</Jhonnys>
180180
</root>

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@
1818
<version>4.12</version>
1919
<scope>compile</scope>
2020
</dependency>
21+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
22+
<dependency>
23+
<groupId>org.apache.commons</groupId>
24+
<artifactId>commons-lang3</artifactId>
25+
<version>3.11</version>
26+
</dependency>
2127
</dependencies>
2228
</project>
Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codepressed.CSVtoXML;
22

3+
import org.apache.commons.lang3.StringUtils;
34
import org.w3c.dom.Document;
45
import javax.xml.parsers.ParserConfigurationException;
56
import javax.xml.transform.TransformerException;
@@ -9,48 +10,53 @@
910
public class Main {
1011
/**
1112
* Executes the CSV to XML conversion
13+
*
14+
* @param args Input file, output file, elements names and csv type.
1215
* @author Daniel Apesteguia Timoner
13-
* @param args Input file, output file and elements names.
1416
*/
15-
public static void main(String[] args){
17+
public static void main(String[] args) {
1618
//Arg validator
17-
if(args.length == 0)
18-
{
19+
if (args.length == 0) {
1920
System.out.println("You didn't type any args.");
2021
System.exit(0);
2122
}
22-
try {
23+
//Vars Initialization
24+
String csvFile = args[0];
25+
String xmlFile = args[1];
26+
String elementName;
27+
String csvSplit = StringUtils.EMPTY;
2328

24-
//Vars Initialization
25-
String csvFile = args[0];
26-
String xmlFile = args[1];
27-
String elementName;
28-
try{
29-
elementName = args[2];}
30-
catch (ArrayIndexOutOfBoundsException e){
31-
System.out.println("You didn't especify any element so we will fix 'element' as parental node.");
32-
elementName = "element";
29+
try {
30+
elementName = args[2];
31+
} catch (ArrayIndexOutOfBoundsException e) {
32+
System.out.println("You didn't especify any element so we will fix 'element' as parental node.");
33+
elementName = "element";
3334
}
3435

36+
try {
37+
if (args[3] == "-s")
38+
csvSplit = ";(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
39+
} catch (ArrayIndexOutOfBoundsException e){
40+
csvSplit = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
41+
}
3542

36-
//ArrayList of ArrayStrings Generation with CSV
37-
ArrayList<String[]> elements;
38-
elements = new Reader().CSVtoArrayList(csvFile);
39-
40-
//XML Doc Generation with ArrayList
41-
Document xmlDoc;
42-
xmlDoc = new TabularToXMLConverter().docBuilder(elements,elementName);
43-
TabularToXMLConverter.transformDocToFile(xmlDoc,xmlFile);
43+
try {
44+
//ArrayList of ArrayStrings Generation with CSV
45+
ArrayList<String[]> elements;
46+
elements = new Reader().CSVtoArrayList(csvFile, csvSplit);
4447

45-
} catch (IOException e) {
46-
e.printStackTrace();
47-
System.out.println("File wasn't found, error: "+e);
48-
}
49-
catch (TransformerException e){
50-
System.out.println("Transformer error: "+e);
51-
}
52-
catch (ParserConfigurationException e) {
53-
System.out.println("Configuration error: "+e);
54-
}
48+
//XML Doc Generation with ArrayList
49+
Document xmlDoc;
50+
xmlDoc = new XMLDoc().docBuilder(elements, elementName);
51+
XMLTransformer.transformDocToFile(xmlDoc, xmlFile);
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
System.out.println("File wasn't found, error: " + e);
55+
} catch (TransformerException e) {
56+
System.out.println("Transformer error: " + e);
57+
} catch (ParserConfigurationException e) {
58+
System.out.println("Configuration error: " + e);
59+
}
5560
}
5661
}
62+

src/main/java/com/codepressed/CSVtoXML/Reader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
public class Reader {
88

99
//ArrayList Generation ArrayStrings with the CSV Data
10-
public ArrayList<String[]> CSVtoArrayList(String csvFile) throws IOException {
10+
public ArrayList<String[]> CSVtoArrayList(String csvFile, String csvSplit) throws IOException {
1111
ArrayList<String[]> elements = new ArrayList<String[]>();
12-
String csvSplit = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
1312
BufferedReader csvReader = null;
1413
String line;
1514

Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
package com.codepressed.CSVtoXML;
22

3+
import org.apache.commons.lang3.StringUtils;
34
import org.w3c.dom.Document;
45
import org.w3c.dom.Element;
56
import org.w3c.dom.Text;
67

78
import javax.xml.parsers.DocumentBuilder;
89
import javax.xml.parsers.DocumentBuilderFactory;
910
import javax.xml.parsers.ParserConfigurationException;
10-
import javax.xml.transform.*;
11-
import javax.xml.transform.dom.DOMSource;
12-
import javax.xml.transform.stream.StreamResult;
13-
import java.io.File;
14-
import java.io.FileNotFoundException;
15-
import java.io.FileOutputStream;
1611
import java.util.ArrayList;
1712

18-
public class TabularToXMLConverter {
13+
public class XMLDoc {
1914

2015
//DOC Generation -> XML with ArrayList String elements
2116
public Document docBuilder(ArrayList<String[]> XMLelements, String elementName) throws ParserConfigurationException {
@@ -31,17 +26,17 @@ public Document docBuilder(ArrayList<String[]> XMLelements, String elementName)
3126
Element mainElement = xmlDoc.createElement(elementName+"s");
3227
rootElement.appendChild(mainElement);
3328

34-
boolean headerDefined = false; //First while will be to define header
35-
String[] header = new String[XMLelements.size()]; //Header initialization
29+
boolean headerDefined = false;
30+
String[] header = new String[XMLelements.size()];
3631

3732
//DOC Generation -> XML Generation of every ELEMENT
38-
for (String[] node : XMLelements) { //FOR every ArrayString
33+
for (String[] node : XMLelements) {
3934
if (headerDefined) {
4035
Element nodesElements = xmlDoc.createElement(elementName);
4136
mainElement.appendChild(nodesElements);
4237

4338
for (int j = 0; j < node.length; j++) {
44-
node[j] = node[j].replaceAll("\"", "").trim();
39+
node[j] = node[j].replaceAll("\"", StringUtils.EMPTY).trim();
4540
Element nodesValues = xmlDoc.createElement(header[j]);
4641
nodesElements.appendChild(nodesValues);
4742
Text nodeTxt = xmlDoc.createTextNode(node[j]);
@@ -52,7 +47,7 @@ public Document docBuilder(ArrayList<String[]> XMLelements, String elementName)
5247
else {
5348
header = node;
5449
for (int j = 0; j < node.length; j++) {
55-
header[j] = header[j].replaceAll("[^a-zA-Z0-9]", "");
50+
header[j] = header[j].replaceAll("[^a-zA-Z0-9]", StringUtils.EMPTY);
5651
//Avoid a fullint
5752
try {
5853
Integer.parseInt(header[j]);
@@ -66,22 +61,5 @@ public Document docBuilder(ArrayList<String[]> XMLelements, String elementName)
6661
return (xmlDoc);
6762
}
6863

69-
//XML Generation -> Transform DOC Data to XML Format
70-
public static void transformDocToFile(Document xmlDoc, String xmlFile) throws TransformerException {
71-
TransformerFactory xmlTransformerFactory = TransformerFactory.newInstance();
72-
Transformer xmlTransformer = xmlTransformerFactory.newTransformer();
73-
xmlTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
74-
xmlTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
75-
xmlTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
76-
xmlTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
77-
FileOutputStream outputStream = null;
78-
try {
79-
outputStream = new FileOutputStream((new File(xmlFile)));
80-
} catch (FileNotFoundException e) {
81-
e.printStackTrace();
82-
}
83-
xmlTransformer.transform(new DOMSource(xmlDoc), new StreamResult(outputStream));
84-
}
85-
8664
}
8765

0 commit comments

Comments
 (0)
0