java如何把txt文件转换成xml文件3
发布网友
发布时间:2023-10-08 17:57
我来回答
共3个回答
热心网友
时间:2024-12-01 18:51
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<temp.length;i++){time.setText(temp[0]);ip.setText(temp[1]);httpType.setText(temp[2]);url.setText(temp[3]);httplevel.setText(temp[4]);linuxVersion.setText(temp[9]);os.setText(temp[10]);AndroidVersion.setText(temp[12]);phoneType.setText(temp[13]);}line = br.readLine();}Writer filewriter = new FileWriter("D:\\log\\test.xml");XMLWriter xmlWriter = new XMLWriter(filewriter);xmlWriter.write(document);xmlWriter.close();}public static void main(String args[]) throws IOException{promble04.txtToXML("D:\\log\\test.txt");}}转换结果如下
<?xml version="1.0" encoding="UTF-8"?><list><log><time>[06/Mar/2013:18:43:55]</time><ip>110.110.1.1</ip><httpType>GET</httpType><url>/rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0/</url><httplevel>HTTP/1.1</httplevel><linuxVersion>'Dalvik/1.4.0</linuxVersion><os>(Linux;</os><AndroidVersion>Android</AndroidVersion><phoneType>2.3.5;</phoneType></log><log><time>[06/Mar/2013:18:43:55]</time><ip>110.110.1.1</ip><httpType>GET</httpType><url>/rest/keyword/getallids/aid/3/os/android2.3.4/ver/1.0/</url><httplevel>HTTP/1.1</httplevel><linuxVersion>'Dalvik/1.4.0</linuxVersion><os>(Linux;</os><AndroidVersion>Android</AndroidVersion><phoneType>2.3.4;</phoneType></log><log><time>[06/Mar/2013:18:43:55]</time><ip>110.110.1.1</ip><httpType>GET</httpType><url>/rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0/</url><httplevel>HTTP/1.1</httplevel><linuxVersion>'Dalvik/1.4.0</linuxVersion><os>(Linux;</os><AndroidVersion>Android</AndroidVersion><phoneType>2.3.5;</phoneType></log><log><time>[06/Mar/2013:18:43:55]</time><ip>110.110.1.1</ip><httpType>GET</httpType><url>/rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0/</url><httplevel>HTTP/1.1</httplevel><linuxVersion>'Dalvik/1.4.0</linuxVersion><os>(Linux;</os><AndroidVersion>Android</AndroidVersion><phoneType>2.3.6;</phoneType></log><log><time>[06/Mar/2013:18:43:55]</time><ip>110.110.1.1</ip><httpType>POST</httpType><url>/rest/keyword/getallids/aid/3/os/android4.0.3/ver/1.0/</url><httplevel>HTTP/1.1</httplevel><linuxVersion>'Dalvik/1.6.0</linuxVersion><os>(Linux;</os><AndroidVersion>Android</AndroidVersion><phoneType>4.0.3;</phoneType></log><log><time>[06/Mar/2013:18:43:55]</time><ip>110.110.1.1</ip><httpType>GET</httpType><url>/rest/keyword/getallids/aid/3/os/android2.2.2/ver/1.0/</url><httplevel>HTTP/1.1</httplevel><linuxVersion>'Dalvik/1.2.0</linuxVersion><os>(Linux;</os><AndroidVersion>Android</AndroidVersion><phoneType>2.2.2;</phoneType></log><log><time>[06/Mar/2013:18:43:55]</time><ip>110.110.1.1</ip><httpType>GET</httpType><url>/rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0/</url><httplevel>HTTP/1.0</httplevel><linuxVersion>'Dalvik/1.4.0</linuxVersion><os>(Linux;</os><AndroidVersion>Android</AndroidVersion><phoneType>2.3.6;</phoneType></log><log><time>[06/Mar/2013:18:43:55]</time><ip>110.110.1.1</ip><httpType>POST</httpType><url>/rest/keyword/getallids/aid/3/os/android4.1.1/ver/1.0/</url><httplevel>HTTP/1.1</httplevel><linuxVersion>'Dalvik/1.6.0</linuxVersion><os>(Linux;</os><AndroidVersion>Android</AndroidVersion><phoneType>4.1.1;</phoneType></log></list>
求采纳为满意回答。
热心网友
时间:2024-12-01 18:51
我就给你说一下思路吧!
首先要获取文本内容,然后分割文本内容成一个个字符串,最后利用DOM操作创建节点,将文本内容一个一个存进去,形成XML文档。
热心网友
时间:2024-12-01 18:51
把txt文件内容读出来,重新写入xml文件追问以什么方式去读?写入到xml文件时,节点怎么加进去?
追答要看你txt文件的内容格式,如果txt文件里有xml标签,那么直接用流读取写入,如果没有xml标签,那么需要用流读取,把流转化为字符串,解析字符串,然后用dom写入到xml文件中