把Txt文件转换成Xml文件

前端之家收集整理的这篇文章主要介绍了把Txt文件转换成Xml文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.utils; 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;//定义一个Txt文件 private String strXmlFileName;//定义一个Xml文件 public TxtToXml() { strTxtFileName = new String();//实例化创建一个Txt文件 strXmlFileName = new String();//实例化创建一个Xml文件 } 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=\"utf-8\" standalone=\"yes\"?>"); outXml.newLine(); outXml.write("<AddressBook>"); outXml.newLine(); outXml.write(" <AddressGroup GroupDesc=\"河南\">"); while ((strTmp = inTxt.readLine()) != null) { StringTokenizer strToken = new StringTokenizer(strTmp,","); String arrTmp[]; arrTmp = new String[1]; for (int i = 0; i < arrTmp.length; i++) arrTmp[i] = new String(""); int index = 0; while (strToken.hasMoreElements()) { strTmp = (String) strToken.nextElement(); strTmp = strTmp.trim(); arrTmp[index++] = strTmp; } outXml.newLine(); outXml.write(" <AddressList Mobile=\"" + arrTmp[0] + "\" UserName=\"1\" UserDesc=\"\"/>"); } outXml.newLine(); outXml.write(" </AddressGroup>"); outXml.newLine(); outXml.write("</AddressBook>"); outXml.flush(); inTxt.close(); outXml.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String txtName = "D:/testtxt.txt"; // txt测试文件 String xmlName = "D:/testxml.xml"; // 转换后成功生成的xml文件 TxtToXml thisClass = new TxtToXml(); thisClass.createXml(txtName,xmlName); } } 原文链接:https://www.f2er.com/xml/296590.html

猜你在找的XML相关文章