简体   繁体   中英

Java - convert pipe and comma delimited file into xml

I want to convert pipe and comma both delimited file into a xml. The delimited file I am looking to convert is in the format:

   UUID  |  Location |   Name  |  Age, UUID  |    Location   |   Name   | Age
   123   | Bangalore |  Paras  |  23, 234    |    Bangalore  | Varun    |  25

And I am looking to convert it to:

<data>
  <UUID>123</UUID> 
  <location>Bangalore</location>
  <name> Paras  </name>
  <age> 23</age>
</data>
<data>
  <UUID>234</UUID> 
  <location>Bangalore</location>
  <name> Paras  </name>
  <age> 23</age>
</data>

You need to double parse your input string. Below sample will help you to get going.

import java.io.BufferedOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.StaxDriver;



class Data {
    String UUID;
    String location;
    String name;
    String age;

    public Data(String[] elements){
        int i = 0;
        UUID = elements[i++].trim();
        location = elements[i++].trim();
        name  = elements[i++].trim();
        age = elements[i++].trim();
    }

}

and Converter

public class Convert {

    static final String[] headers = {"UUID","location","name","age"};


    public static void main(String[] args) throws IOException {

        List<Data> dataList = new ArrayList<Data>();
        LineNumberReader r = new LineNumberReader(new FileReader("test.csv"));

        String line = r.readLine();
        while(line != null){
            String[] dataArray = line.split(",");
            for(String record:dataArray){
                String[] elements = record.split("\\|");
                Data data = new Data(elements);
                //If you dont want to accumulate data then you can stream xml out. 
                dataList.add(data);
            }
            line = r.readLine();
        }
        r.close();

        // now use your favorite xml library to stream out. I use xstream. in your case even direct constructing xml seems ok too. 
        XStream xstream = new XStream(new StaxDriver());
        xstream.alias("data", Data.class);
        xstream.toXML(dataList, System.out);

        //if you want formatted xml then use PrettyPrintWriter..
/*      BufferedOutputStream stdout = new BufferedOutputStream(System.out);
        PrettyPrintWriter p = new PrettyPrintWriter(new OutputStreamWriter(stdout));
        xstream.marshal(dataList, p);
*/      
    }

}

My sample csv file is

123|    Bangalore  |  Paras  |  23  , 234    |    Bangalore  | Varun    |  25
456|    Mumbai  |  xyz  |  30  , 999    |    Ahmedabad  | abc    |  28

and output is

<list>
  <data>
    <UUID>123</UUID>
    <location>Bangalore</location>
    <name>Paras</name>
    <age>23</age>
  </data>
  <data>
    <UUID>234</UUID>
    <location>Bangalore</location>
    <name>Varun</name>
    <age>25</age>
  </data>
  <data>
    <UUID>456</UUID>
    <location>Mumbai</location>
    <name>xyz</name>
    <age>30</age>
  </data>
  <data>
    <UUID>999</UUID>
    <location>Ahmedabad</location>
    <name>abc</name>
    <age>28</age>
  </data>
</list>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM