8000 Patch MainReader and Tabular · codepressed/Java-CSV2XML@f405b34 · GitHub
[go: up one dir, main page]

Skip to content

Commit f405b34

Browse files
author
A. Apesteguia
committed
Patch MainReader and Tabular
1 parent 88715df commit f405b34

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
public class Main {
1010
/**
1111
* Executes the CSV to XML conversion
12-
* @param args file input and output
13-
*
12+
* @author Daniel Apesteguia Timoner
13+
* @param args Input file, output file and elements names.
1414
*/
1515
public static void main(String[] args){
1616
//Arg validator
@@ -24,14 +24,21 @@ public static void main(String[] args){
2424
//Vars Initialization
2525
String csvFile = args[0];
2626
String xmlFile = args[1];
27-
String elementName = args[2];
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";
33+
}
34+
2835

2936
//ArrayList of ArrayStrings Generation with CSV
30-
ArrayList<String[]> elements = null;
37+
ArrayList<String[]> elements;
3138
elements = new Reader().CSVtoArrayList(csvFile);
3239

3340
//XML Doc Generation with ArrayList
34-
Document xmlDoc = null;
41+
Document xmlDoc;
3542
xmlDoc = new TabularToXMLConverter().docBuilder(elements,elementName);
3643
TabularToXMLConverter.transformDocToFile(xmlDoc,xmlFile);
3744

@@ -40,7 +47,7 @@ public static void main(String[] args){
4047
System.out.println("File wasn't found, error: "+e);
4148
}
4249
catch (TransformerException e){
43-
System.out.println("Transformer error: "+e);;
50+
System.out.println("Transformer error: "+e);
4451
}
4552
catch (ParserConfigurationException e) {
4653
System.out.println("Configuration error: "+e);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public class Reader {
1010
public ArrayList<String[]> CSVtoArrayList(String csvFile) throws IOException {
1111
ArrayList<String[]> elements = new ArrayList<String[]>();
1212
String csvSplit = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
13-
String line = "";
1413
BufferedReader csvReader = null;
14+
String line;
1515

1616
try {
1717
csvReader = new BufferedReader(new FileReader(csvFile));

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

Lines changed: 3 additions & 1 deletion
< 99B5 td data-grid-cell-id="diff-7824b0c52ddd86c5f0199f34a6adcf854b6af9f7b4bab97f4abf5b5f77619a58-20-20-2" data-line-anchor="diff-7824b0c52ddd86c5f0199f34a6adcf854b6af9f7b4bab97f4abf5b5f77619a58R20" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">
//DOC Generation -> XML with ArrayList String elements
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ public class TabularToXMLConverter {
1919

2020
2121
public Document docBuilder(ArrayList<String[]> XMLelements, String elementName) throws ParserConfigurationException {
22+
if (elementName == null){
23+
elementName = "element";
24+
}
2225
DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance();
2326
DocumentBuilder xmlBuilder = xmlFactory.newDocumentBuilder();
2427
Document xmlDoc = xmlBuilder.newDocument();
2528

2629
Element rootElement = xmlDoc.createElement("root");
2730
xmlDoc.appendChild(rootElement);
28-
2931
Element mainElement = xmlDoc.createElement(elementName+"s");
3032
rootElement.appendChild(mainElement);
3133

0 commit comments

Comments
 (0)
0