Skip to content

Commit

Permalink
Major update
Browse files Browse the repository at this point in the history
  • Loading branch information
A. Apesteguia committed Dec 1, 2020
1 parent f405b34 commit 87aaf9a
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 98 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* Uses the traditional methodology to generate the xml (without a library), through
of nodes and tree structure.
* Can read ANY CSV File

* Args are: Input file, output file, element node name and optionally, if you add -s program will read the csv
as semicolon-separated values instead of comma-separated values.
## Project test

### CSV Input File
Expand Down
68 changes: 34 additions & 34 deletions examples/Grades.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<elements>
<element>
<Jhonnys>
<Jhonny>
<Lastname>Alfalfa</Lastname>
<Firstname>Aloysius</Firstname>
<SSN>123-45-6789</SSN>
Expand All @@ -11,8 +11,8 @@
<Test4>83.0</Test4>
<Final>49.0</Final>
<Grades>D-</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Alfred</Lastname>
<Firstname>University</Firstname>
<SSN>123-12-1234</SSN>
Expand All @@ -22,8 +22,8 @@
<Test4>97.0</Test4>
<Final>48.0</Final>
<Grades>D+</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Gerty</Lastname>
<Firstname>Gramma</Firstname>
<SSN>567-89-0123</SSN>
Expand All @@ -33,8 +33,8 @@
<Test4>40.0</Test4>
<Final>44.0</Final>
<Grades>C</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Android</Lastname>
<Firstname>Electric</Firstname>
<SSN>087-65-4321</SSN>
Expand All @@ -44,8 +44,8 @@
<Test4>45.0</Test4>
<Final>47.0</Final>
<Grades>B-</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Bumpkin</Lastname>
<Firstname>Fred</Firstname>
<SSN>456-78-9012</SSN>
Expand All @@ -55,8 +55,8 @@
<Test4>77.0</Test4>
<Final>45.0</Final>
<Grades>A-</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Rubble</Lastname>
<Firstname>Betty</Firstname>
<SSN>234-56-7890</SSN>
Expand All @@ -66,8 +66,8 @@
<Test4>90.0</Test4>
<Final>46.0</Final>
<Grades>C-</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Noshow</Lastname>
<Firstname>Cecil</Firstname>
<SSN>345-67-8901</SSN>
Expand All @@ -77,8 +77,8 @@
<Test4>4.0</Test4>
<Final>43.0</Final>
<Grades>F</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Buff</Lastname>
<Firstname>Bif</Firstname>
<SSN>632-79-9939</SSN>
Expand All @@ -88,8 +88,8 @@
<Test4>40.0</Test4>
<Final>50.0</Final>
<Grades>B+</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Airpump</Lastname>
<Firstname>Andrew</Firstname>
<SSN>223-45-6789</SSN>
Expand All @@ -98,8 +98,8 @@
<Test3>100.0</Test3>
<Test4>83.0</Test4>
<Final>A</Final>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Backus</Lastname>
<Firstname>Jim</Firstname>
<SSN>143-12-1234</SSN>
Expand All @@ -109,8 +109,8 @@
<Test4>96.0</Test4>
<Final>97.0</Final>
<Grades>A+</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Carnivore</Lastname>
<Firstname>Art</Firstname>
<SSN>565-89-0123</SSN>
Expand All @@ -120,8 +120,8 @@
<Test4>60.0</Test4>
<Final>40.0</Final>
<Grades>D+</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Dandy</Lastname>
<Firstname>Jim</Firstname>
<SSN>087-75-4321</SSN>
Expand All @@ -131,8 +131,8 @@
<Test4>36.0</Test4>
<Final>45.0</Final>
<Grades>C+</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Elephant</Lastname>
<Firstname>Ima</Firstname>
<SSN>456-71-9012</SSN>
Expand All @@ -142,8 +142,8 @@
<Test4>88.0</Test4>
<Final>77.0</Final>
<Grades>B-</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Franklin</Lastname>
<Firstname>Benny</Firstname>
<SSN>234-56-2890</SSN>
Expand All @@ -153,8 +153,8 @@
<Test4>80.0</Test4>
<Final>90.0</Final>
<Grades>B-</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>George</Lastname>
<Firstname>Boy</Firstname>
<SSN>345-67-3901</SSN>
Expand All @@ -164,8 +164,8 @@
<Test4>-1.0</Test4>
<Final>4.0</Final>
<Grades>B</Grades>
</element>
<element>
</Jhonny>
<Jhonny>
<Lastname>Heffalump</Lastname>
<Firstname>Harvey</Firstname>
<SSN>632-79-9439</SSN>
Expand All @@ -175,6 +175,6 @@
<Test4>30.0</Test4>
<Final>40.0</Final>
<Grades>C</Grades>
</element>
</elements>
</Jhonny>
</Jhonnys>
</root>
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@
<version>4.12</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
</dependencies>
</project>
70 changes: 38 additions & 32 deletions src/main/java/com/codepressed/CSVtoXML/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.codepressed.CSVtoXML;

import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
Expand All @@ -9,48 +10,53 @@
public class Main {
/**
* Executes the CSV to XML conversion
*
* @param args Input file, output file, elements names and csv type.
* @author Daniel Apesteguia Timoner
* @param args Input file, output file and elements names.
*/
public static void main(String[] args){
public static void main(String[] args) {
//Arg validator
if(args.length == 0)
{
if (args.length == 0) {
System.out.println("You didn't type any args.");
System.exit(0);
}
try {
//Vars Initialization
String csvFile = args[0];
String xmlFile = args[1];
String elementName;
String csvSplit = StringUtils.EMPTY;

//Vars Initialization
String csvFile = args[0];
String xmlFile = args[1];
String elementName;
try{
elementName = args[2];}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("You didn't especify any element so we will fix 'element' as parental node.");
elementName = "element";
try {
elementName = args[2];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("You didn't especify any element so we will fix 'element' as parental node.");
elementName = "element";
}

try {
if (args[3] == "-s")
csvSplit = ";(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
} catch (ArrayIndexOutOfBoundsException e){
csvSplit = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
}

//ArrayList of ArrayStrings Generation with CSV
ArrayList<String[]> elements;
elements = new Reader().CSVtoArrayList(csvFile);

//XML Doc Generation with ArrayList
Document xmlDoc;
xmlDoc = new TabularToXMLConverter().docBuilder(elements,elementName);
TabularToXMLConverter.transformDocToFile(xmlDoc,xmlFile);
try {
//ArrayList of ArrayStrings Generation with CSV
ArrayList<String[]> elements;
elements = new Reader().CSVtoArrayList(csvFile, csvSplit);

} catch (IOException e) {
e.printStackTrace();
System.out.println("File wasn't found, error: "+e);
}
catch (TransformerException e){
System.out.println("Transformer error: "+e);
}
catch (ParserConfigurationException e) {
System.out.println("Configuration error: "+e);
}
//XML Doc Generation with ArrayList
Document xmlDoc;
xmlDoc = new XMLDoc().docBuilder(elements, elementName);
XMLTransformer.transformDocToFile(xmlDoc, xmlFile);
} catch (IOException e) {
e.printStackTrace();
System.out.println("File wasn't found, error: " + e);
} catch (TransformerException e) {
System.out.println("Transformer error: " + e);
} catch (ParserConfigurationException e) {
System.out.println("Configuration error: " + e);
}
}
}

3 changes: 1 addition & 2 deletions src/main/java/com/codepressed/CSVtoXML/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
public class Reader {

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package com.codepressed.CSVtoXML;

import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;

public class TabularToXMLConverter {
public class XMLDoc {

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

boolean headerDefined = false; //First while will be to define header
String[] header = new String[XMLelements.size()]; //Header initialization
boolean headerDefined = false;
String[] header = new String[XMLelements.size()];

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

for (int j = 0; j < node.length; j++) {
node[j] = node[j].replaceAll("\"", "").trim();
node[j] = node[j].replaceAll("\"", StringUtils.EMPTY).trim();
Element nodesValues = xmlDoc.createElement(header[j]);
nodesElements.appendChild(nodesValues);
Text nodeTxt = xmlDoc.createTextNode(node[j]);
Expand All @@ -52,7 +47,7 @@ public Document docBuilder(ArrayList<String[]> XMLelements, String elementName)
else {
header = node;
for (int j = 0; j < node.length; j++) {
header[j] = header[j].replaceAll("[^a-zA-Z0-9]", "");
header[j] = header[j].replaceAll("[^a-zA-Z0-9]", StringUtils.EMPTY);
//Avoid a fullint
try {
Integer.parseInt(header[j]);
Expand All @@ -66,22 +61,5 @@ public Document docBuilder(ArrayList<String[]> XMLelements, String elementName)
return (xmlDoc);
}

//XML Generation -> Transform DOC Data to XML Format
public static void transformDocToFile(Document xmlDoc, String xmlFile) throws TransformerException {
TransformerFactory xmlTransformerFactory = TransformerFactory.newInstance();
Transformer xmlTransformer = xmlTransformerFactory.newTransformer();
xmlTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
xmlTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
xmlTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
xmlTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream((new File(xmlFile)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
xmlTransformer.transform(new DOMSource(xmlDoc), new StreamResult(outputStream));
}

}

Loading

0 comments on commit 87aaf9a

Please sign in to comment.