dom4j可以试试,不知道你的数据量有多大,如果太大的话,我没试过
xml文件是有规律的,你可以把要导出的数据构造一下,
我有个简单的代码
package com.test.xmlimport 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.OutputFormat
import org.dom4j.io.XMLWriter
public class Dom4JXML {
public void createXML() {
//用工厂类创建一个document实例
Document doc = DocumentHelper.createDocument()
//创建根元素emps
Element rootEle = doc.addElement("emps")
//添加注释
rootEle.addComment("这是一个dom4j生成的xml文件")
//emps根节点下创建一个emp节点
Element empEle = rootEle.addElement("emp")
//emp添加属性id="1"
empEle.addAttribute("id", "1")
//emp节点下创建一个name节点
Element nameEle = empEle.addElement("name")
//name节点下创建一个文本节点zhangsan
nameEle.setText("zhangsan")
//再为name节点创建一个兄弟节点
Element sexEle = empEle.addElement("sex")
sexEle.setText("man")
//将document中的内容写入文件中
try {
Writer out = new FileWriter("F:\\emps.xml")
//格式化输出,类型IE浏览一样
OutputFormat format = OutputFormat.createPrettyPrint()
//OutputFormat format = OutputFormat.createCompactFormat()
format.setEncoding("UTF-8")
//创建写出对象
XMLWriter writer = new XMLWriter(out,format)
writer.write(doc)
writer.close()
System.out.println("生成emps.xml成功。")
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
System.out.println("失败了。")
}
}
public static void main(String[] args) {
new Dom4JXML().createXML()
}
}
然而从xml文件中解析以后的数据收集可以用SAX试试看
我这里有个简单的例子
package com.test.xmlimport org.xml.sax.Attributes
import org.xml.sax.SAXException
import org.xml.sax.helpers.DefaultHandler
public class SAXParseHandler extends DefaultHandler{
public void startDocument()throws SAXException{
System.out.println("起始文挡")
}
public void endDocument()throws SAXException{
System.out.println("结束文挡")
}
public void characters(char[] ch,int start,int length)throws SAXException{
String charString=new String(ch,start,length)
System.out.println("字符:"+charString)
}
public void startElement(String namespaceURI,String localName,String qName,Attributes atts)throws SAXException{
System.out.println("起始元素:"+qName)
for(int i=0i<atts.getLength()i++){
System.out.println("属性值:"+atts.getValue(i))
}
}
public void endElement(String namespaceURI,String localName,String qName)throws SAXException{
System.out.println("结束元素:"+qName)
}
}
package com.test.xml
import javax.xml.parsers.SAXParser
import javax.xml.parsers.SAXParserFactory
public class Books {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO 自动生成方法存根
SAXParserFactory factory=SAXParserFactory.newInstance()
SAXParser parser=factory.newSAXParser()
parser.parse("booksamp.xml",new SAXParseHandler())
}
}
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 testimport 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 = 0i<temp.lengthi++)
{
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>
2./**1. * desciption:java create xml file
2. * author:maomao
3. * datetime:2007/04/04 23:42
4. */
5.
6.package com.xh.xml
1.
2.import java.io.FileOutputStream
1.import java.io.IOException
1.import org.jdom.Document
1.import org.jdom.Element
1.import org.jdom.JDOMException
1.import org.jdom.output.XMLOutputter
1.
2.public class Java2XML {
1.
2. public void BuildXMLDoc() throws IOException, JDOMException {
3.
4. // 创建根节点 list
5. Element root = new Element("list")
6.
7. // 根节点添加到文档中;
8. Document Doc = new Document(root)
9.
10. // 此处 for 循环可替换成 遍历 数据库表的结果集 *** 作
11. for (int i = 0i <5i++) {
12.
13. // 创建节点 user
14. Element elements = new Element("user")
15.
16. // 给 user 节点添加属性 id
17. elements.setAttribute("id", "" + i)
18.
19. // 给 user 节点添加子节点并赋值;
20. // new Element("name")中的 "name" 替换成表中相应字段,setText("xuehui")中 "xuehui 替换成表中记录值;
21. elements.addContent(new Element("name").setText("xuehui"))
22. elements.addContent(new Element("age").setText("28"))
23. elements.addContent(new Element("sex").setText("Male"))
24.
25. // 给父节点list添加user子节点
26. root.addContent(elements)
27.
28. }
29. XMLOutputter XMLOut = new XMLOutputter()
30.
31. // 输出 user.xml 文件;
32. XMLOut.output(Doc, new FileOutputStream("user.xml"))
33. }
34.
35. public static void main(String[] args) {
36. try {
37. Java2XML j2x = new Java2XML()
38. System.out.println("生成 mxl 文件...")
39. j2x.BuildXMLDoc()
40. } catch (Exception e) {
41. e.printStackTrace()
42. }
43. }
44.
45.}
生成结果:
# <?xml version="1.0" encoding="UTF-8"?>
# <list>
# <user id="0">
# <name>xuehui</name>
# <age>28</age>
# <sex>Male</sex>
# </user>
# <user id="1">
# <name>xuehui</name>
# <age>28</age>
# <sex>Male</sex>
# </user>
# <user id="2">
# <name>xuehui</name>
# <age>28</age>
# <sex>Male</sex>
# </user>
# <user id="3">
# <name>xuehui</name>
# <age>28</age>
# <sex>Male</sex>
# </user>
# <user id="4">
# <name>xuehui</name>
# <age>28</age>
# <sex>Male</sex>
# </user>
# </list>
你可以参考一下链接,然后拷贝代码,这里我直接拷贝带上行号。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)