Java - 将管道和逗号分隔的文件转换为xml

时间:2015-09-25 15:59:56

标签: java xml

我想将管道和逗号分隔文件转换为xml。我想要转换的分隔文件格式为:

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

我希望将其转换为:

<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>

1 个答案:

答案 0 :(得分:0)

您需要双重解析输入字符串。下面的示例将帮助您开始。

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();
    }

}

和转换器

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);
*/      
    }

}

我的示例csv文件是

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

,输出

<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>