日期:2014-05-20  浏览次数:20742 次

xml读写
哪位高手自己用流与集合写过读取xml的类文件请发给小弟(要求不用第三方组件和java自带的dom sax读取,自己用流读),不胜感激


--------------------------------
以下内容为自动编辑的内容,并非楼主的发贴内容,此仅用于显示而已,并无任何其他特殊作用
楼主【pjkimi】截止到2008-08-05 13:38:36的历史汇总数据(不包括此帖):
发帖的总数量:17 发帖的总分数:485 每贴平均分数:28  
回帖的总数量:19 得分贴总数量:5 回帖的得分率:26%  
结贴的总数量:17 结贴的总分数:485  
无满意结贴数:1 无满意结贴分:20  
未结的帖子数:0 未结的总分数:0  
结贴的百分比:100.00% 结分的百分比:100.00%  
无满意结贴率:5.88 % 无满意结分率:4.12 %  
敬礼!
取消马甲机器人,请点这里:http://www.java2000.net/mycsdn/robotStop.jsp?usern=pjkimi

------解决方案--------------------
源码没有
不过我写的这个代码你可以参考
http://blog.csdn.net/yirentianran/archive/2008/07/24/2704446.aspx
用正则解析xml就行了 
至于输入输出流 你自己网上找一下
很多的

------解决方案--------------------
Java code

package 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\"?>");
   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";
  TxtToXml thisClass = new TxtToXml();
  thisClass.createXml(txtName, xmlName);
 }
}