java,php,asp.net,linux,javascript,mysql,mssql,oracle,编程

简单的txt转换xml

转载请注明:http://www.pmjava.com/Article/ShowInfo.asp?ID=56553
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(
"");
   outXml.newLine();
   outXml.write(
" ");
  
while ((strTmp = inTxt.readLine()) != null) {
    StringTokenizer strToken
= new StringTokenizer(strTmp, "");
    String arrTmp[];
    arrTmp
= new String[3];
   
for (int i = 0; i < 3; i++)
     arrTmp[i]
= new String("");
   
int index = 0;
    outXml.newLine();
    outXml.write(
"    ");
   
while (strToken.hasMoreElements()) {
     strTmp
= (String) strToken.nextElement();
     strTmp
= strTmp.trim();
     arrTmp[index
++] = strTmp;
    }
    outXml.newLine();
    outXml.write(
"        " + arrTmp[0] + "");
    outXml.newLine();
    outXml.write(
"        " + arrTmp[1] + "");
    outXml.newLine();
    outXml.write(
"        " + arrTmp[2] + "");
    outXml.newLine();
    outXml.write(
"    ");
   }
   outXml.newLine();
   outXml.write(
" ");
   outXml.flush();
  }
catch (Exception e) {
   e.printStackTrace();
  }
}

public static void main(String[] args) {
  String txtName
= "testtxt.txt";
  String xmlName
= "testxml.xml";
  TxtToXml thisClass
= new TxtToXml();
  thisClass.createXml(txtName, xmlName);
}
}

posted on 2009-06-10 21:14 rrong_m 阅读(1612) 评论(0)  编辑  收藏

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

随笔档案

文章分类

文章档案

java编程

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜