永发信息网

java如何把txt文件转换成xml文件

答案:3  悬赏:60  手机版
解决时间 2021-01-13 02:20
  • 提问者网友:喧嚣尘世
  • 2021-01-12 04:33
java如何把txt文件转换成xml文件
最佳答案
  • 五星知识达人网友:洎扰庸人
  • 2021-01-12 05:42
把txt文件内容读出来,重新写入xml文件追问以什么方式去读?写入到xml文件时,节点怎么加进去?追答要看你txt文件的内容格式,如果txt文件里有xml标签,那么直接用流读取写入,如果没有xml标签,那么需要用流读取,把流转化为字符串,解析字符串,然后用dom写入到xml文件中
全部回答
  • 1楼网友:不甚了了
  • 2021-01-12 06:58
我就给你说一下思路吧!
首先要获取文本内容,然后分割文本内容成一个个字符串,最后利用DOM操作创建节点,将文本内容一个一个存进去,形成XML文档。
  • 2楼网友:未来江山和你
  • 2021-01-12 06:10
txt文件格式如下,采用的是一个服务端tomcat的log日志
[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0/ HTTP/1.1 200 210 1453 1.453 'Dalvik/1.4.0 (Linux; U; Android 2.3.5; T550 Build/MocorDroid2.3.5)'[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.3.4/ver/1.0/ HTTP/1.1 200 210 1453 1.453 'Dalvik/1.4.0 (Linux; U; Android 2.3.4; LG-E730 Build/GRJ22)'[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0/ HTTP/1.1 200 210 1453 1.453 'Dalvik/1.4.0 (Linux; U; Android 2.3.5; MI-ONE Plus Build/GINGERBREAD)'[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0/ HTTP/1.1 200 210 1453 1.453 'Dalvik/1.4.0 (Linux; U; Android 2.3.6; GT-I9220 Build/GINGERBREAD)'[06/Mar/2013:18:43:55] 110.110.1.1 POST /rest/keyword/getallids/aid/3/os/android4.0.3/ver/1.0/ HTTP/1.1 200 210 1469 1.469 'Dalvik/1.6.0 (Linux; U; Android 4.0.3; HUAWEI C8812 Build/HuaweiC8812)'[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.2.2/ver/1.0/ HTTP/1.1 200 210 1453 1.453 'Dalvik/1.2.0 (Linux; U; Android 2.2.2; HUAWEI T8300 Build/FRF91)'[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0/ HTTP/1.0 200 210 1484 1.484 'Dalvik/1.4.0 (Linux; U; Android 2.3.6; Mytel Build/GRK39F)'[06/Mar/2013:18:43:55] 110.110.1.1 POST /rest/keyword/getallids/aid/3/os/android4.1.1/ver/1.0/ HTTP/1.1 200 210 1406 1.406 'Dalvik/1.6.0 (Linux; U; Android 4.1.1; GT-N7100 Build/JRO03C)'转换代码如下
package test;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Writer;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.XMLWriter;public class promble04 {@SuppressWarnings("resource") public static void txtToXML(String txtPath) throws IOException {File file = new File(txtPath);Document document = DocumentHelper.createDocument();Element list = document.addElement("list");BufferedReader br = new BufferedReader(new FileReader(file));String line = br.readLine();while(line!=null){ String temp[] = line.split(" "); Element log = list.addElement("log"); Element time = log.addElement("time"); Element ip = log.addElement("ip"); Element httpType = log.addElement("httpType"); Element url = log.addElement("url"); Element httplevel = log.addElement("httplevel"); Element linuxVersion = log.addElement("linuxVersion"); Element os = log.addElement("os"); Element AndroidVersion = log.addElement("AndroidVersion"); Element phoneType = log.addElement("phoneType"); for(int i = 0;i 110.110.1.1GET/rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0/HTTP/1.1'Dalvik/1.4.0(Linux;Android2.3.5; 110.110.1.1GET/rest/keyword/getallids/aid/3/os/android2.3.4/ver/1.0/HTTP/1.1'Dalvik/1.4.0(Linux;Android2.3.4; 110.110.1.1GET/rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0/HTTP/1.1'Dalvik/1.4.0(Linux;Android2.3.5; 110.110.1.1GET/rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0/HTTP/1.1'Dalvik/1.4.0(Linux;Android2.3.6; 110.110.1.1POST/rest/keyword/getallids/aid/3/os/android4.0.3/ver/1.0/HTTP/1.1'Dalvik/1.6.0(Linux;Android4.0.3; 110.110.1.1GET/rest/keyword/getallids/aid/3/os/android2.2.2/ver/1.0/HTTP/1.1'Dalvik/1.2.0(Linux;Android2.2.2; 110.110.1.1GET/rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0/HTTP/1.0'Dalvik/1.4.0(Linux;Android2.3.6; 110.110.1.1POST/rest/keyword/getallids/aid/3/os/android4.1.1/ver/1.0/HTTP/1.1'Dalvik/1.6.0(Linux;Android4.1.1;
求采纳为满意回答。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯