Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read and write csv and xml file

I’m currently working on a data processing project in Java and need to handle both XML and CSV files. I’m relatively new to file manipulation in Python and looking for guidance on efficiently reading and writing data in these formats. Here are my specific queries.

Only Java standard libs are acceptable. Because the version dependence of external libs are too complex

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

write csv file

try {
        // Join the data elements into CSV-formatted lines
        List<String> lines = data.stream()
                .map(line -> String.join(",", line))
                .collect(Collectors.toList());

        // Write the lines to the CSV file
        Files.write(Paths.get(csvFilePath), lines);
    } catch (IOException e) {
        e.printStackTrace();
    }

each data is a liststring and the data variable should be list of list string

read csv

try {
        List<String> lines = Files.lines(Paths.get(csvFilePath))
                .collect(Collectors.toList());

        for (String line : lines) {
            // Split the line into fields based on the CSV format (comma-separated)
            String[] fields = line.split(",");
            
            // Process the fields as needed
            for (String field : fields) {
                System.out.print(field + " ");
            }
            System.out.println(); // Move to the next line for the next record
        }

csvfilePath is the file path which you want to read

write to XML

try {
        // Create a DocumentBuilder
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        // Create a new Document
        Document document = builder.newDocument();

        // Create root element
        Element rootElement = document.createElement("people");
        document.appendChild(rootElement);

        // Create person elements
        Element person1 = document.createElement("person");
        Element name1 = document.createElement("name");
        Element age1 = document.createElement("age");

        name1.appendChild(document.createTextNode("John Doe"));
        age1.appendChild(document.createTextNode("30"));

        person1.appendChild(name1);
        person1.appendChild(age1);

        Element person2 = document.createElement("person");
        Element name2 = document.createElement("name");
        Element age2 = document.createElement("age");

        name2.appendChild(document.createTextNode("Alice Smith"));
        age2.appendChild(document.createTextNode("25"));

        person2.appendChild(name2);
        person2.appendChild(age2);

        rootElement.appendChild(person1);
        rootElement.appendChild(person2);

        // Write the XML content to a file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File("path/to/your/output/file.xml"));
        transformer.transform(source, result);

        System.out.println("XML data written to file");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Hope it’s helpful

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading