0

I am developing a Java application that reads a .CSV file, displays the content of a GUI textarea and convert ths content to XML data(prints XML on a textarea as well) this XML data is now transformed using XSLT.

My application accepts a .CSV file, converting comma separated values data to XML has been a challenge for me. I have read loads of materials on it and I still haven't grasped the concept yet. Can anyone direct me to how I can do this?

jonsca
  • 10,218
  • 26
  • 54
  • 62
Abiodun
  • 959
  • 6
  • 17
  • 38
  • 1
    It would be nice if you try something by your own... then ask for improvements... this will (a) give you a better understanding of the monster that you are dealing with, (b) help us to answer the road block instead of solving the whole task for you. Got it? Read http://whathaveyoutried.com – Nishant Aug 11 '12 at 14:26

2 Answers2

0

You should make a java class that implements Serializable. Then as you read the csv file in, populate each field in that class. Then you can use the Java XMLEncoder to write to an XML file like this.

    XMLEncoder encoder = null;
            MyClass data = new MyClass();
            data.setField1("field 1 from csv");

    try {
        encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("c:/myfile.xml")));
        encoder.writeObject(data);
    } catch (final IOException e) {
        logger.error(e.getMessage());
    } finally {
        if (encoder != null) {
            encoder.close();
        }
    }
Logan
  • 2,369
  • 19
  • 20
  • thanx alot Logan..my system is working fine but am having issues with the with the xslt aspect of it. – Abiodun Aug 19 '12 at 20:43
0

From your question I read, that you're already to process the csv files and that you're xml schema is already defined (you mentioned an xslt that operates on the result of the csv->xml transformation).

I'd recommend using a small xml library like dom4j to create the xml document. The quick start guide for dom4j has a short example that shows the steps for Creating a new XML document and Converting to and from Strings.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268