java解析xml文件

package com.longyi.citicoa.domain.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
import com.longyi.citicoa.domain.exception.FileWriteReadException;
import com.longyi.citicoa.domain.model.TaskLine;
import com.longyi.citicoa.domain.model.TaskRect;

/**
* <p>Title: XmlUtil.java</p>
*
* <p>Description:XML工具类 </p>
*
* <p>Date: May 5, 2011</p>
*
* <p>Time: 11:38:51 AM</p>
*
* <p>Copyright: 2011</p>
*
* <p>Company: huajun</p>
*
* @version 1.0
*
* <p>============================================</p>
* <p>Modification History
* <p>Mender: </p>
* <p>Date: </p>
* <p>Reason: </p>
* <p>============================================</p>
*/
public class XmlUtil {

/**
* 解析xml生成任务模板
* @param xml
* @throws DocumentException
*/
@SuppressWarnings("unchecked")
public static Map<String, List> convertXmlToDesign(String xml) throws FileWriteReadException {
try {
if (xml == null || "".equals(xml.trim()))
return null;
Map<String, List> map = new HashMap<String, List>();
Document doc = DocumentHelper.parseText(xml);
// 任务模板 begin
List<TaskRect> trlist = null;
List<Node> taskList = doc.selectNodes("/root/task");
if (taskList != null && taskList.size() > 0) {
trlist = new ArrayList<TaskRect>();
for (Node taskNode : taskList) {
TaskRect tr = new TaskRect();
Node name = taskNode.selectSingleNode("name");
Node key = taskNode.selectSingleNode("key");
Node initiator = taskNode.selectSingleNode("initiator");
Node initiate_dept = taskNode.selectSingleNode("initiate_dept");
Node dept_chief = taskNode.selectSingleNode("dept_chief");
Node all_user = taskNode.selectSingleNode("all_user");
Node choose_other = taskNode.selectSingleNode("choose_other");
Node member_list = taskNode.selectSingleNode("member_list");
Node crew_list = taskNode.selectSingleNode("crew_list");
Node claim_model = taskNode.selectSingleNode("claim_model");
Node transdept = taskNode.selectSingleNode("transdept");
Node field_list = taskNode.selectSingleNode("field_list");
Node procdure_id = taskNode.selectSingleNode("procdure_id");
Node file_id = taskNode.selectSingleNode("file_id");
tr.setName(name.getStringValue());
tr.setKey_id(key.getStringValue());
tr.setInitiator(Integer.valueOf(initiator.getStringValue()));
tr.setInitiate_dept(Integer.valueOf(initiate_dept.getStringValue()));
tr.setDept_chief(Integer.valueOf(dept_chief.getStringValue()));
tr.setAll_user(Integer.valueOf(all_user.getStringValue()));
tr.setChoose_other(Integer.valueOf(choose_other.getStringValue()));
tr.setMember_list(member_list.getStringValue());
tr.setCrew_list(crew_list.getStringValue());
tr.setClaim_model(Integer.valueOf(claim_model.getStringValue()));
tr.setTransdept(transdept.getStringValue());
tr.setField_list(field_list.getStringValue());
String proce=procdure_id.getStringValue();
String file=file_id.getStringValue();
if(proce!=null&&!"".endsWith(proce)){
tr.setProcdure_id(Integer.valueOf(proce));
}else
{
tr.setProcdure_id(null);
}
if(file!=null && !"".endsWith(file))
{
tr.setFile_id(Integer.valueOf(file));
}else
{tr.setFile_id(null);}
trlist.add(tr);
}
}
// 任务模板 end
// 任务流向 begin
List<TaskLine> tllist = null;
List<Node> lineList = doc.selectNodes("/root/line");
if (lineList != null && lineList.size() > 0) {
tllist = new ArrayList<TaskLine>();
for (Node lineNode : lineList) {
TaskLine tl = new TaskLine();
Node frompoint = lineNode.selectSingleNode("frompoint");
Node topoint = lineNode.selectSingleNode("topoint");
tl.setFrompoint_key(frompoint.getStringValue());
tl.setTopoint_key(topoint.getStringValue());
tllist.add(tl);
}
}
// 任务流向 end
map.put("task", trlist);
map.put("line", tllist);
return map;
} catch (NumberFormatException e) {
e.printStackTrace();
throw new FileWriteReadException(e.getMessage());
} catch (DocumentException e) {
e.printStackTrace();
throw new FileWriteReadException(e.getMessage());
}
}

}
[/size][size=x-small][color=blue]
[/color][/size][size=x-small]

你可能感兴趣的:(解析xml)