Working from home - Simple XML to CSV file Conversion Application

in #java4 years ago

As much as working from home is interesting, it still has one or two disadvantages of its own. Since the beginning of this whole virus outbreak saga, I've had to work from home which means most of the regular daily tasks that I carry out on the office desk are now done on the bed.

Well things got a little bit boring recently and due to an issue with one of our dev-ops my boss had to hand over a project to me, he's been working with C# all this while and I'm better with Java so I had to rewrite the whole code in java which at first was annoying due to the fact that I wanted to break it all down to different classes and follow good programming practices, turned out I was wasting too much time getting it done so I just wrote all the codes in two classes.


I wrote one class for the configuration and the other for picking, conversion, and dropping of files.

Let's do a quick run-through of the configuration class:

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Setting {

    String inputFilePath;
    String outputFilePath;
    File file;
    String headers;
    String XMLElements;
    NodeList nodeList;

    public void applyConfig() {

        file = new File("Settings.config");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            // an instance of builder to parse the specified xml file
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();

            nodeList = doc.getElementsByTagName(doc.getDocumentElement().getNodeName());

            for (int itr = 0; itr < nodeList.getLength(); itr++) {
                Node node = nodeList.item(itr);
                System.out.println("\nNode Name :" + node.getNodeName());

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) node;
                    inputFilePath = eElement.getElementsByTagName("InputFilePath").item(0).getTextContent();
                    outputFilePath = eElement.getElementsByTagName("OutputFilePath").item(0).getTextContent();
                    XMLElements = eElement.getElementsByTagName("XMLHeader").item(0).getTextContent();
                    headers = eElement.getElementsByTagName("CSVHeaders").item(0).getTextContent();
                    
                    if (inputFilePath.equals("")) {
                        System.out.println("Please set Input File Path");
                    }
                    if (outputFilePath.equals("")) {
                        System.out.println("Please set Output File Path");
                    }
                    if (XMLElements.equals("")) {
                        System.out.println("Please set XML Elements");
                    }
                    if (headers.equals("")) {
                        System.out.println("Please set CSV Headers");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            }
    }

    public String getInputFilePath() {
        return inputFilePath;
    }

    public Setting() {
    }

    public String getOutputFilePath() {
        return outputFilePath;
    }

    public String getHeaders() {
        return headers;
    }

    public String getXMLElements() {
        return XMLElements;
    }
}

Here's what happened up there, I pick the Settings.config file from the same path as the jar file of this program, the file is written in XML format as seen below;

<?xml version="1.0"?>  
<Settings>  
 
    <InputFilePath>C:\\workspace\\BFSS\\Resources\\</InputFilePath>
    <OutputFilePath>C:\\workspace\\BFSS\\Resources\\</OutputFilePath>
 
    <XMLHeader></XMLHeader>
    <CSVHeaders></CSVHeaders>
    
  
</Settings>  

This specifies the path to pick the input XML file from and where to place the CSV file created from the content of the XML file. Since the program is not for me I had to do this just to help whoever will be using the program to use it with ease.

After picking the settings file, we make use of the DocumentBuilderFactory pick the content out of the file and load them into the application.

Next is picking the Node in the document, for this we have a NodeList which picks all the nodes in the document and then we have elements that help pick the content in the node and make them available for use in the program.

That's all this settings class does.

I'll have to stop here for now and continue another time since I have to return to work.