java dom4j read xml

使用dom4j读取xml文件其实很简单。

例如xml如下:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!– definitions of work-flows –>
<WorkFlowList>
<WorkFlow id=”1″ tag=”TEST” name=”测试”>
<node index=”1″ tag=”” name=”录入” IsStart=”true”>
<process name=”上报” tag=”Report” next=”2″ Function=”Org=Self();Position=3C-ZR;”></process>
</node>
<node index=”2″ tag=”” name=”审核”>
<process name=”驳回” tag=”Reject” next=”1″ Function=”Org=Sql(select fromorgid from sys_t_flowhis where recordid=’$RecordId’ order by totime desc);Position=History()”></process>
<process name=”审核” tag=”Verify” next=”7″ Function=”Org=Up();Position=3C-ZR”></process>
</node>
<node index=”7″ tag=”” name=”完成” IsEnd=”true”>
</node>
</WorkFlow>
<WorkFlow id=”001″ tag=”WTK” name=”问题库管理”>
<node index=”1″ tag=”” name=”问题录入” IsStart=”true”>
<process name=”保存” tag=”Save” next=”1″ Function=”Org=Self();Position=Self();User=Self()”></process>
<process name=”分发” tag=”Distri” next=”2″ Function=”Org=Self();Position=3C-ZR;”></process>
</node>
<node index=”2″ tag=”” name=”问题已分发”>
<process name=”驳回” tag=”Reject” next=”1″ Function=”User=History()”></process>
<process name=”分发” tag=”Distri” next=”2″
Function=”Position=3C-ZR;Org=Up();User=$User”></process>
<process name=”解决” tag=”Resolve” next=”3″ Function=”User=$User;Org=Self()”></process>
</node>
<node index=”3″ tag=”” name=”问题已解决”>
<process name=”通过” tag=”Pass” next=”4″></process>
<process name=”不通过” tag=”Reject” next=”2″ Function=”User=$History”></process>
</node>
<node index=”4″ tag=”” name=”问题已销号” IsEnd=”true” />
</WorkFlow>
<WorkFlow id=”002″ tag=”WXTC” name=”维修天窗”>
<node index=”1″ tag=”” name=”等待工区上报” IsStart=”true”>
<process name=”上报” tag=”Report” next=”2″ Function=”Position=zhuren;Org=Up()”></process>
</node>
<node index=”2″ tag=”” name=”等待车间审核”>
<process name=”审核” tag=”Verify” next=”3″ Function=”Org=Up()”></process>
</node>
<node index=”3″ tag=”” name=”等待安调科审核”>
<process name=”审核” tag=”Verify” next=”10″ Function=”Org=Self()”></process>
</node>
<node index=”10″ tag=”” name=”等待安调科导出”>
<process name=”导出” tag=”Export” next=”11″ Function=”Org=Self()”></process>
</node>
<node index=”11″ tag=”” name=”等待安调科导入”>
<process name=”导入” tag=”Import” next=”5″ Function=”Org=Self()”></process>
</node>
<node index=”5″ tag=”” name=”等待安调科下发”>
<process name=”下发” tag=”Distri” next=”6″ Function=”Position=$History;Org=$History”></process>
</node>
<node index=”6″ tag=”” name=”等待车间填写卡控”>
<process name=”下发车间” tag=”DisCJ” next=”7″ Function=”Org=Self()”></process>
<process name=”下发工区” tag=”DisGQ” next=”8″ Function=”Position=$History;Org=$History”></process>
</node>
<node index=”7″ tag=”” name=”等待车间填写完成情况”>
<process name=”完成” tag=”Finish” next=”9″></process>
</node>
<node index=”8″ tag=”” name=”等待工区填写完成情况”>
<process name=”完成” tag=”Finish” next=”9″></process>
</node>
<node index=”9″ tag=”” name=”完成” IsEnd=”true”>
</node>
</WorkFlow>
</WorkFlowList>

读取:

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

private static final String XML = ReadProperty.getKey(“xmlfile”);

SAXReader reader = new SAXReader();
Document document = reader.read(new File(XML));
Element root = document.getRootElement();

if (root != null) {
for (Iterator<Element> flowIterator = root.elementIterator();flowIterator.hasNext();) {
Element e = flowIterator.next();
System.out.println(e.attributeValue(“id”));//id属性

List<Attribute> aList = element.attributes();//属性列表

List<Element> nodeList = e.elements();//二级节点
}
}

This entry was posted in Computer, Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s