JAVA读取本地TXT文件转换为XML文件输出

高分请分析下,JAVA读取本地TXT文件转换为XML文件输出?

TXT文件中每行有10列,转成XML后每行作为一个子结构。。最好能给一个详细的列子,谢谢~~
最新回答
寄烟念七晴

2024-10-24 04:49:11

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-10-24 06:48:38

很简单,你用InputStream读取到tet的byte数组,转换成字符串,然后用outputStream以byte形式写入就可以了
作业,快到粪坑来

2024-10-24 07:39:23

这个不难啊,
具体txt每行是什么结构? 要转成的xml是什么结构?
要的话给我mail 5069029@qq.com