first class Menu (renamed fenetre here)
<pre lang="Java">
package image;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Window.Type;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
import com.opencsv.exceptions.CsvException;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class fenetre extends conversion {
private JFrame frmFaiaPourFidexpert;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
fenetre window = new fenetre();
window.frmFaiaPourFidexpert.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public fenetre() {
initialize();
}
private void initialize() {
frmFaiaPourFidexpert = new JFrame();
frmFaiaPourFidexpert.setBackground(new Color(245, 222, 179));
frmFaiaPourFidexpert.setTitle("FAIA pour FID-Expert");
frmFaiaPourFidexpert.setForeground(Color.BLACK);
frmFaiaPourFidexpert.setIconImage(Toolkit.getDefaultToolkit().getImage(fenetre.class.getResource("/image/lux.png")));
frmFaiaPourFidexpert.setBounds(100, 100, 470, 440);
frmFaiaPourFidexpert.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmFaiaPourFidexpert.setResizable(false);
JButton btnNewButton = new JButton("Convertir un fichier");
btnNewButton.setBounds(0, 302, 456, 29);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("Convertir un fichier")) {
JFileChooser fichier = new JFileChooser( FileSystemView.getFileSystemView().getHomeDirectory());
fichier.setDialogTitle("Choississez votre fichier .csv");
fichier.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Fichier .csv","csv");
fichier.addChoosableFileFilter(filter);
int res = fichier.showOpenDialog(null);
if (res == JFileChooser.APPROVE_OPTION) {
String XML;
XML = null;
String fi = fichier.getName();
XMLCreators xmlCreators = new XMLCreators();
xmlCreators.convertFile(fi, XML, '\t');
}
;
}
}
});
frmFaiaPourFidexpert.getContentPane().setLayout(null);
btnNewButton.setForeground(Color.BLACK);
btnNewButton.setFont(new Font("Yu Gothic UI", Font.BOLD, 14));
btnNewButton.setBackground(new Color(168, 146, 28));
frmFaiaPourFidexpert.getContentPane().add(btnNewButton);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setBounds(0, 14, 456, 328);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setIcon(new ImageIcon(fenetre.class.getResource("/image/fid.png")));
frmFaiaPourFidexpert.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel(" Conversion fichier pour FAIA");
lblNewLabel_1.setBounds(0, 0, 456, 24);
lblNewLabel_1.setBackground(new Color(218, 165, 32));
lblNewLabel_1.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 18));
frmFaiaPourFidexpert.getContentPane().add(lblNewLabel_1);
JMenuBar menuBar = new JMenuBar();
frmFaiaPourFidexpert.setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Aide");
JMenuItem menuItem = new JMenuItem("?");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String w = e.getActionCommand();
JFrame frame = new JFrame();
if (w.equals("?")) {
JDialog d = new JDialog(frame, "Aide");
JLabel l = new JLabel("Le bouton convertit un fichier .csv en .xml directement sur votre PC");
d.getContentPane().add(l);
d.setIconImage(Toolkit.getDefaultToolkit().getImage(fenetre.class.getResource("/image/lux.png")));
d.setSize(400,100);
d.setVisible(true);
}
}
});
mnNewMenu.add(menuItem);
menuBar.add(mnNewMenu);
}
}
and the conversion class
<pre>package image;
import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class conversion {
public static void main(String[] args) {
new conversion();
}
public class XMLCreators {
protected DocumentBuilderFactory domFactory = null;
protected DocumentBuilder domBuilder = null;
public XMLCreators() {
try {
domFactory = DocumentBuilderFactory.newInstance();
domBuilder = domFactory.newDocumentBuilder();
} catch (FactoryConfigurationError exp) {
System.err.println(exp.toString());
} catch (ParserConfigurationException exp) {
System.err.println(exp.toString());
} catch (Exception exp) {
System.err.println(exp.toString());
}
}
public int convertFile(String csvFileName, String xmlFileName, char delimiter) {
int rowsCount = -1;
BufferedReader csvReader;
System.out.println(csvFileName)
System.out.println(csvReader)
try {
Document newDoc = domBuilder.newDocument();
Element rootElement = newDoc.createElement("XMLCreators");
newDoc.appendChild(rootElement);
csvReader = new BufferedReader(new FileReader(csvFileName));
System.out.println(csvFileName)
CSVParser parser = new CSVParserBuilder()
.withSeparator(delimiter)
.build();
CSVReader reader = new CSVReaderBuilder(new FileReader(csvFileName))
.withCSVParser(parser)
.build();
String[] nextLine;
int line = 0;
List<String> headers = new ArrayList<String>(5);
while ((nextLine = reader.readNext()) != null) {
if (line == 0) {
for (String col : nextLine) {
headers.add(col);
}
} else {
Element rowElement = newDoc.createElement("row");
rootElement.appendChild(rowElement);
int col = 0;
for (String value : nextLine) {
String header = headers.get(col).replaceAll("[\\t\\p{Zs}\\u0020]", "_");
Element curElement = newDoc.createElement(header);
curElement.appendChild(newDoc.createTextNode(value.trim()));
rowElement.appendChild(curElement);
col++;
}
}
line++;
}
FileWriter writer = null;
try {
writer = new FileWriter(new File(xmlFileName));
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
aTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
aTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
Source src = new DOMSource(newDoc);
Result result = new StreamResult(writer);
aTransformer.transform(src, result);
writer.flush();
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
writer.close();
} catch (Exception e) {
}
}
} catch (IOException exp) {
exp.printStackTrace();
} catch (Exception exp) {
exp.printStackTrace();
}
return rowsCount;
}
}
}