txt转xml类

package XML类;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;

public class txt转xml类 {
	private String strTxtFileName;

	 private String strXmlFileName;

	 public txt转xml类() {
	  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\"?>");
	   outXml.newLine();
	   outXml.write("<people>");
	   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("    <students>");
	    while (strToken.hasMoreElements()) {
	     strTmp = (String) strToken.nextElement();
	     strTmp = strTmp.trim();
	     arrTmp[index++] = strTmp;
	    }
	    outXml.newLine();
	    outXml.write("        <name>" + arrTmp[0] + "</name>");
	    outXml.newLine();
	    outXml.write("        <sex>" + arrTmp[1] + "</sex>");
	    outXml.newLine();
	    outXml.write("        <age>" + arrTmp[2] + "</age>");
	    outXml.newLine();
	    outXml.write("    </students>");
	   }
	   outXml.newLine();
	   outXml.write("</people>");
	   outXml.flush();
	  } catch (Exception e) {
	   e.printStackTrace();
	  }
	 }

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

你可能感兴趣的:(txt转xml类)