lundi 17 août 2020

How to add a attribute based on a certain condition to a xml using java

I need to create a xml using java

Input from a csv file looks like :

Peter,35
Peter,45 
Peter,55

John,40
John,55
John,15

Dave,32
Dave,20

It must add the attribute only if the mark is greater than 30

The output must look like the below one

<main>
  <data student="Peter">
   <subject marks="35"/>
   <subject marks="45"/>
   <subject marks="55"/>
  </data>
  <data student="John">
   <subject marks="40"/>
   <subject marks="55"/>
  </data>
  <data student="John">
   <subject marks="32"/>
  </data>
</main>   

but i am getting a output like the below one. It is creating data student="Peter" 3 times but i want it only once

<?xml version="1.0" encoding="UTF-8"?>
<main>
<data student="Peter">
<subject marks="35"/>
</data>
<data student="Peter">
<subject marks="45"/>
</data>
<data student="Peter">
<subject marks="55"/>
</data>
<data student="John">
<subject marks="40"/>
</data>
<data student="John">
<subject marks="55"/>
</data>
<data student="Dave">
<subject marks="32"/>
</data>
</main>

My code looks like the below one

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CreateXml {

    public static void main(String argv[]) throws DOMException, IOException {

      try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("main");
        doc.appendChild(rootElement); 
        
        String line = "";  
        BufferedReader br = new BufferedReader(new FileReader("E:\\file.csv"));  
        while ((line = br.readLine()) != null) 
        {  
        String[] values = line.split(","); 
        if(Integer.parseInt(values[1])>30) {
        Element data = doc.createElement("data");
        rootElement.appendChild(data);
        data.setAttribute("student", values[0]);
        
        Element subject = doc.createElement("subject");
        data.appendChild(subject);
        subject.setAttribute("marks", values[1]);
        }
        }  
        
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("E:\\output.xml"));

        transformer.transform(source, result);

      } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
      } catch (TransformerException tfe) {
        tfe.printStackTrace();
      }
    }
}

Can anyone help me with this?

Aucun commentaire:

Enregistrer un commentaire