求助java读取txt文件转化为xml形式输出

题目:将exercise.txt文件内容转化为xml形式txt文件里都是注册表文件转换的,内容应该都是注册表的如下[HKEY_USERS][HKEY_USERS\.DEF... 题目:将exercise.txt文件内容转化为xml形式
txt文件里都是注册表文件转换的,内容应该都是注册表的如下
[HKEY_USERS]
[HKEY_USERS\.DEFAULT]
[HKEY_USERS\.DEFAULT\Control Panel]
[HKEY_USERS\.DEFAULT\Control Panel\Accessibility]
[HKEY_USERS\.DEFAULT\Control Panel\Accessibility\Blind Access]
"On"="0"
[HKEY_USERS\.DEFAULT\Control Panel\Accessibility\HighContrast]
"Flags"="126"
"High Contrast Scheme"=""
[HKEY_USERS\.DEFAULT\Control Panel\Accessibility\Keyboard Preference]
"On"="0"
展开
 我来答
高质量团赛
推荐于2016-08-20 · 知道合伙人软件行家
高质量团赛
知道合伙人软件行家
采纳数:5899 获赞数:97602
获得全国计算机应用技术证书

向TA提问 私信TA
展开全部

txt文件格式如下,采用的是一个服务端tomcat的log日志

12345678[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<img id="selectsearch-icon" alt="搜索" src="https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/iknow/qb/select-search.png">/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; <a class="baidu-highlight" href="https://www.baidu.com/s?wd=MI-ONE&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9myDvrH63nADYPhRsrycv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1RLrHmzPj0LPf" target="_blank">MI-ONE</a> <a class="baidu-highlight" href="https://www.baidu.com/s?wd=Plus&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9myDvrH63nADYPhRsrycv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1RLrHmzPj0LPf" target="_blank">Plus</a> 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; <a class="baidu-highlight" href="https://www.baidu.com/s?wd=HUAWEI&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9myDvrH63nADYPhRsrycv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1RLrHmzPj0LPf" target="_blank">HUAWEI</a> <a class="baidu-highlight" href="https://www.baidu.com/s?wd=C8812&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9myDvrH63nADYPhRsrycv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1RLrHmzPj0LPf" target="_blank">C8812</a> Build/<a class="baidu-highlight" href="https://www.baidu.com/s?wd=Huawei&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9myDvrH63nADYPhRsrycv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1RLrHmzPj0LPf" target="_blank">Huawei</a><a class="baidu-highlight" href="https://www.baidu.com/s?wd=C8812&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9myDvrH63nADYPhRsrycv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1RLrHmzPj0LPf" target="_blank">C8812</a>)'[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; <a class="baidu-highlight" href="https://www.baidu.com/s?wd=HUAWEI&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9myDvrH63nADYPhRsrycv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1RLrHmzPj0LPf" target="_blank">HUAWEI</a> 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)'

转换代码如下

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172package 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");    }}

转换结果如下

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091<?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>
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式