Thursday, May 31, 2007

试用dom4j

以前用过jdom
感觉也算是很方便的

只是后来据说jdom的效率没有dom4j好
于是就一直都想看看
今天就初试了下,感觉还是很不错的

===================测试xml:http://ipis007.spaces.live.com/blog/cns!75F0E4FEE274140B!1721.entry


===================测试代码
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* @author justin ( mailto:xuzc@carnation.com.cn )
*
*/
public class TestParse {
/**
*
*/
public TestParse() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// //Bof: justin 20070531. read the file to the string, then use the string to parse.
// BufferedReader br = new BufferedReader(new FileReader(
// "E://24th_task/xmlTest/type1.xml"));
// StringBuffer sb = new StringBuffer();
// String line = null;
// while ((line = br.readLine()) != null) {
// sb.append(line);
// }
// line = sb.toString();
// //Eof: justin done here. 20070531.
// //Bof: justin 20070531. parse the string to the xml document.
// Document document = DocumentHelper.parseText(line);
// //Eof: justin done here. 20070531.
//Bof: justin 20070531. use this method just to parse the document directly, without any process.
SAXReader reader = new SAXReader();
Document document = reader.read(new File("E://24th_task/xmlTest/type2.xml"));
//Eof: justin done here. 20070531.
//Bof: justin 20070531. init a root element.
Element root = document.getRootElement();
//Eof: justin done here. 20070531.
System.out.println("parse the root information...");
Iterator ab = root.attributeIterator();
while(ab.hasNext()){
Attribute a = (Attribute) ab.next();
System.out.println("attribute:"+a.getName()+",value:"+a.getValue());
}
System.out.println("");
System.out.println("parse the handler information...");
Iterator handlers = root.elementIterator("handler");
while(handlers.hasNext()){
Element el = (Element) handlers.next();
Iterator handlerAttributes = el.attributeIterator();
while(handlerAttributes.hasNext()){
Attribute handlerAttribute = (Attribute) handlerAttributes.next();
System.out.println("attribute:"+handlerAttribute.getName()+",value:"+handlerAttribute.getValue());
}
System.out.println("");
}
System.out.println("");
System.out.println("parse the header....");
Element header = root.element("header");
Iterator headerDetail = header.elementIterator();
while(headerDetail.hasNext()){
Element headerXml = (Element) headerDetail.next();
Iterator headerAttributes = headerXml.attributeIterator();
while(headerAttributes.hasNext()){
Attribute handlerAttribute = (Attribute) headerAttributes.next();
System.out.println("attribute:"+handlerAttribute.getName()+",value:"+handlerAttribute.getValue());
}
System.out.println("");
}
System.out.println("");
System.out.println("parse the body.....");
Element body = root.element("body");
Iterator bodyDetail = body.elementIterator();
while(bodyDetail.hasNext()){
Element bodyXml = (Element) bodyDetail.next();
Iterator bodyAttributes = bodyXml.attributeIterator();
while(bodyAttributes.hasNext()){
Attribute handlerAttribute = (Attribute) bodyAttributes.next();
System.out.println("attribute:"+handlerAttribute.getName()+",value:"+handlerAttribute.getValue());
}
System.out.println("");
}
} catch (Exception e) {
e.printStackTrace();
}
}
===================输出
parse the root information...
attribute:name,value:reckoning
attribute:cn_name,value:帐单
attribute:table,value:zd
parse the handler information...
attribute:type,value:TypeNotMatch
attribute:handler,value:gzbill.handler.TypeNotMatchHandler
attribute:type,value:EmptyString
attribute:handler,value:gzbill.handler.EmptyStringHandler

parse the header....
attribute:name,value:total_line
attribute:length,value:9
attribute:type,value:int
attribute:required,value:1
attribute:cn_name,value:总行数
attribute:name,value:total_amount
attribute:length,value:20
attribute:type,value:float
attribute:required,value:1
attribute:cn_name,value:总金额

parse the body.....
attribute:name,value:user_equipment_num
attribute:length,value:10
attribute:type,value:int
attribute:required,value:1
attribute:cnName,value:用户设备序号
attribute:filed,value:yhsbxh
attribute:name,value:district
attribute:length,value:4
attribute:type,value:char
attribute:required,value:1
attribute:cnName,value:地区
attribute:filed,value:dq
attribute:name,value:business_type
attribute:length,value:2
attribute:type,value:char
attribute:required,value:1
attribute:cnName,value:业务类别
attribute:filed,value:ywlb
===================
一开始试用读取字符串,然后再对字符串进行parse,这个过程,我的文本文件是ansii的,但是读取的时候不存在任何问题
但是,如果我是用:
SAXReader reader = new SAXReader();
Document document = reader.read(new File());
系统提示一个错误:Invalid byte 2 of 2-byte UTF-8 sequence
然后,我用记事本打开那个xml文件,把它另存,更改为UTF-8编码,重新运行程序,bingle,程序正确输出。
--------------总结
还是很方便的,dom4j,看起来真的很方便。和jdom差不多。不用不要引用错类哦。
--------------项目地址:
http://www.dom4j.org/
http://sourceforge.net/project/showfiles.php?group_id=16035

2 comments:

joeventus said...

我也申请了个blogger的account,但没空搞啊。。

joeventus said...
This comment has been removed by the author.