Java - 将管道和逗号分隔的文件转换为 xml
Posted
技术标签:
【中文标题】Java - 将管道和逗号分隔的文件转换为 xml【英文标题】:Java - convert pipe and comma delimited file into xml 【发布时间】:2015-09-25 15:59:55 【问题描述】:我想将管道和逗号都分隔的文件转换为 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>
【问题讨论】:
你有什么问题? java中有什么方法可以做这样的事情,我知道将csv转换为xml,但我的文件包含两者。 请将其添加到您的帖子中,并向我们展示您的尝试并解释失败的原因。 读取文件并使用一些xml库来编写xml文件。我相信您首选的搜索引擎会为您提供帮助。 【参考方案1】:您需要双重解析您的输入字符串。下面的示例将帮助您开始。
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>
【讨论】:
并添加任何验证(如 null 等)。以上是关于Java - 将管道和逗号分隔的文件转换为 xml的主要内容,如果未能解决你的问题,请参考以下文章
python 从KeepassX转换导出的XML文件,可以将其导入1Password(通过CSV逗号分隔文件)
如何将同时具有逗号和空格分隔符的 CSV 文件转换为只有空格分隔符的 csv