从Java中的文本文件生成XML
这是我的文本文件:从Java中的文本文件生成XML,java,xml,io,xstream,Java,Xml,Io,Xstream,这是我的文本文件: 5625145214 6 8562320154 2 8542154157 5 6325145214 5 5214214584 6 5625142224 3 8562456754 1 我想使用XStream生成XML文件: 这是我的代码: private static void generateXml() throws IOException { XStream xStream = new XStream(new DomDriver()); Stri
5625145214 6
8562320154 2
8542154157 5
6325145214 5
5214214584 6
5625142224 3
8562456754 1
我想使用XStream
生成XML
文件:
这是我的代码:
private static void generateXml() throws IOException {
XStream xStream = new XStream(new DomDriver());
String line = null;
try (BufferedReader br = new BufferedReader(new FileReader("Unique Numbers.txt"))) {
while ((line = br.readLine()) != null) {
String xml = xStream.toXML(line);
System.out.println(xml);
}
}
}
如何生成
xml
文件?我需要它。我不知道您想要什么样的xml,但是下面的代码:
public static void main(String[] args) {
generateXml();
}
private static void generateXml() {
XStream xStream = new XStream(new DomDriver());
String line = null;
try{
BufferedReader br = new BufferedReader(new FileReader(new File("Unique Numbers.txt"))) ;
while ((line = br.readLine()) != null) {
String xml = xStream.toXML(line);
System.out.println(xml);
}
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
将打印:
<string>5625145214 6</string>
<string>8562320154 2</string>
<string>8542154157 5</string>
<string>6325145214 5</string>
<string>5214214584 6</string>
<string>5625142224 3</string>
<string>8562456754 1</string>
5625145214 6
8562320154 2
8542154157 5
6325145214 5
5214214584 6
5625142224 3
8562456754 1
我应该将此xml写入文件吗?如果愿意,可以。这取决于你想要实现什么。