/*
* 该程序代码用来解决xml文件的转换
* 将txt文件按照相应的格式转换为xml文件
*/
package xmlwenjian;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.io.IOUtils;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.xml.sax.InputSource;
public class Test {
/*
* 下面的方法用作将txt文件转化为字符串
* 参数1:txt文件所在路径
* 参数2:编码方式
* */
public static String readTxt(String filePath,String encoding){
try {
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = "";
StringBuffer stringBuffer = new StringBuffer();
while((lineTxt = bufferedReader.readLine()) != null){
//System.out.println(lineTxt);
stringBuffer .append(lineTxt);
stringBuffer .append("!!!");
}
read.close();
return stringBuffer.toString();
}
else{
System.out.println("找不到指定的文件");
return null;
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return null;
}
public static String readXmltxt(String filePath,String encoding){
try {
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = "";
StringBuffer stringBuffer = new StringBuffer();
while((lineTxt = bufferedReader.readLine()) != null){
//System.out.println(lineTxt);
stringBuffer .append(lineTxt);
}
read.close();
//XMLSerializer xmlSerializer = new XMLSerializer();
return stringBuffer.toString();
}
else{
System.out.println("找不到指定的文件");
return null;
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return null;
}
/*
* 将xml字符串转换成json
* */
public static JSONObject getJSONFromXml(String xmlString) {
XMLSerializer xmlSerializer = new XMLSerializer();
JSONObject json = (JSONObject) xmlSerializer.read(xmlString);
return json;
}
public static void txt2Xml(String examplePath,String txtPath,String xmlPath) throws JDOMException, IOException {
/*参数说明:
examplePath:xml模板的路径
txtPath:需要转换的txt文件
xmlPath:转换完成之后的xml文件路径*/
/*读入需要转换的txt文件*/
String readString=readTxt(txtPath,"GBK");
String[] infLine=readString.split("!!!");//将读到的txt文件内容一行行分开
String [] finalString=new String[infLine.length];
for(int count=0;count<infLine.length;count++){
String xmlString=readXmltxt(examplePath, "GBK");
JSONObject json=getJSONFromXml(xmlString);
String result=json.toString();
//System.out.println(result);
String []response=result.split(":\"");
String [] endingStrings=response[response.length-1].split("\"");
String end=endingStrings[endingStrings.length-1];
String [] tt=infLine[count].split("\t");
for(int i=0;i<response.length-1;i++){
response[i]=response[i]+":"+"\""+tt[i]+"\"";
}
for(int i=1;i<response.length-1;i++){
response[i]=response[i].substring(response[i].indexOf("\"")+1);
}
finalString[count]="";
for(int i=0;i<response.length-1;i++){
finalString[count]=finalString[count]+response[i];
}
finalString[count]=finalString[count]+end;
}
JSONArray jj=JSONArray.fromObject(finalString);
XMLSerializer xmlSerializer = new XMLSerializer();
String xml = xmlSerializer.write(jj);
//System.out.println(xml);
StringReader sr = new StringReader(xml);
InputSource is = new InputSource(sr);
Document doc = (new SAXBuilder()).build(is);
//Document doc = (Document) saxreader.read(xml);
Format format = Format.getCompactFormat();
format.setEncoding("UTF-8"); //设置XML文件的字符为UTF-8
format.setIndent(" ");//设置缩进
XMLOutputter outputter = new XMLOutputter(format);
FileWriter writer = new FileWriter(xmlPath);
outputter.output(doc, writer);
writer.close();
}
public static void main(String[] args) throws IOException, JDOMException {
txt2Xml("C:\\Users\\Administrator\\Desktop\\33.xml","C:\\Users\\Administrator\\Desktop\\113.txt","C:\\Users\\Administrator\\Desktop\\df.xml");
}
}