[英]Generation of XML from List of arrays of String in Java
I am using opencsv
to parse text file and generate List<String[]>
now I want to generate XML
from List<String[]>
and so my question is do we have any 3rd Party Libraries
that does that conversion, if not, what would be an better approach to solve the issue. 我正在使用opencsv
解析文本文件并生成List<String[]>
现在我想从List<String[]>
生成XML
,所以我的问题是我们是否有任何进行此转换的3rd Party Libraries
,如果没有,那么将是解决该问题的更好方法。
Here is the Parsing Logic:
这是Parsing Logic:
public class ParseFile {
public ParseFile() {
}
public void getQuotes() {
String fileName = "C:\\GS.txt";
try {
CSVReader reader = new CSVReader(new FileReader(fileName), '\t');
String[] nextLine;
List<String[]> dataList = new ArrayList<String[]>();
dataList = reader.readAll();
} catch (Exception e) {
}
}
public static void main(String args[]) {
ParseFile test = new ParseFile();
test.getQuotes();
System.out.println("Parsing Done Successfully....");
}
}
So my txt
file looks like: 所以我的txt
文件看起来像:
Header Information: ContractDate Trader Quantity
1st Line of Data: 03/23/12 GS 100
and I need to have XML Structure
like: 而且我需要具有以下XML Structure
:
<root>
<entry id='1'>
<ContractDate>03/23/12</ContractDate>
<Trader>GS</Trader>
<Quantity>100</Quantity>
</entry>
</root>
我将使用JAXB- http://jaxb.java.net/-您可以构建一个POJO,使用JAXB对其进行批注,然后将其写入XML。
Your XML is simple enough. 您的XML很简单。 I would just iterate over your data in a loop and write out the XML using a StringBuilder. 我只是循环遍历您的数据,然后使用StringBuilder写出XML。
StringBuilder builder = new StringBuilder();
builder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
builder.append("<root>\n");
int entryId = 1;
for (String[] data : dataList) {
builder.append("\t<entry id='" + entryId++ + "'>\n");
builder.append("\t\t<ContractDate>" + data[0] + "</ContractDate>\n");
// etc
}
builder.append("</root>\n");
You don't explicitly require a 3rd party library to generate your rather simple XML format. 您不需要显式要求第三方库来生成您相当简单的XML格式。 You could simply write a to a StringBuffer object: 您可以简单地将String写入StringBuffer对象:
...
StringBuffer sb = new StringBuffer();
sb.append("<root>");
for (String[] row: dataList) {
sb.append("<entry>");
sb.append("<ContractDate>").append(row[0]).append("</ContractDate>");
// etc...
sb.append("</entry>");
}
sb.append("</root>");
...
However, I'm not necessarily advocating it. 但是,我不一定要提倡它。 I personally recommend Simple XML . 我个人推荐Simple XML 。 Create a POJO to model your rows (I won't include all properties in my example): 创建一个POJO来对行建模(在示例中,我将不包括所有属性):
@Root
public class Entry {
@Element
private String trader;
@Attribute
private int id;
public Entry() {
super();
}
public String getTrader() {
return text;
}
public void setTrader(String trader) {
this.trader = trader;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@Root(name="root")
public class Entries {
@ElementList(inline=true)
List<Entry> entries;
public Entries(List<Entry> entries) {
this.entries = entries;
}
}
It's certainly some extra work up-front, but for non-trivial programs, having proper Java classes to represent the data models makes a lot of sense and provides greater flexibility, and code clarity. 当然,这是一些额外的前期工作,但对于非平凡的程序,具有适当的Java类来表示数据模型很有用,并提供更大的灵活性和代码清晰度。
To save save as XML you do this: 要将另存为XML保存,请执行以下操作:
List<Entry> entryList = new ArrayList();
int entryCount = 0;
Serializer serializer = new Persister();
for (String[] row: dataList) {
Entry entry = new Entry();
entry.setTrader(row[1]);
entry.setId(++entryCount);
entryList.add(entry);
}
File result = new File("entries.xml");
serializer.write(new Entries(entryList), result);
The Simple XML library gives you a lot of flexibility. Simple XML库为您提供了很大的灵活性。 You can also load up the XML file back into a list of Entry objects. 您还可以将XML文件加载回Entry对象列表。
Maybe XStream would good solution for you. 也许XStream将为您提供一个好的解决方案。 It is a great and simple API for read/Write XML and more ... :) 这是一个很棒的简单的API,用于读/写XML以及更多... :)
Your example with XStream: 您的XStream示例:
public class ParseFile {
public ParseFile() {
}
public void getQuotes() {
String fileName = "C:\\GS.txt";
try {
CSVReader reader = new CSVReader(new FileReader(fileName), '\t');
String[] nextLine;
List<String[]> dataList = new ArrayList<String[]>();
dataList = reader.readAll();
} catch (Exception e) {
}
//Convert to XML with XStream
XStream xstream = new XStream();
String xmlContent = xstream.toXML( dataList );
}
public static void main(String args[]) {
ParseFile test = new ParseFile();
test.getQuotes();
System.out.println("Parsing Done Successfully....");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.