下面是把txt文本转换成xml的一个例子,希望对大家有所帮助
package com.liu;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;
public class TxtToXml {
private String strTxtFileName;
private String strXmlFileName;
public TxtToXml() {
strTxtFileName = new String();
strXmlFileName = new String();
}
public void createXml(String strTxt, String strXml) {
strTxtFileName = strTxt;
strXmlFileName = strXml;
String strTmp;
try {
BufferedReader inTxt = new BufferedReader(new FileReader( strTxtFileName)); BufferedWriter outXml = new
BufferedWriter(new FileWriter(
strXmlFileName));
outXml.write("<?xml version= \"1.0\" encoding=\"gb2312\"?>");
wLine();
outXml.write("<people>");
while ((strTmp = adLine()) != null) {
StringTokenizer strToken = new StringTokenizer(strTmp, "");
String arrTmp[];
arrTmp = new String[3];
for (int i = 0; i < 3; i++)
arrTmp[i] = new String("");
chm转换txt int index = 0;
wLine();
outXml.write(" <students>");
while (strToken.hasMoreElements()) {
strTmp = (String) Element();
strTmp = im();
arrTmp[index++] = strTmp;
}
wLine();
outXml.write(" <name>" + arrTmp[0] + "</name>");
wLine();
outXml.write(" <sex>" + arrTmp[1] + "</sex>");
wLine();
outXml.write(" <age>" + arrTmp[2] + "</age>");
wLine();
outXml.write(" </students>");
}
wLine();
outXml.write("</people>");
outXml.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
// 测试一下,完美通过!
public static void main(String[] args) {
String txtName = "";
String xmlName = "l";
TxtToXml thisClass = new TxtToXml();
ateXml(txtName, xmlName);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论