java dom4j 解析xml

第一步获取路径

 

/**
* @Description: get _querySciptMap From InputStream
* @return HashMap
*/
public static HashMap getQueryInfoBeanInfo() {
HashMap infoMap = new HashMap();
try {
PathMatchingResourcePatternResolver pp = new PathMatchingResourcePatternResolver();
String configUrl = "classpath*:/db.xml";//xml文件在项目路径
Resource[] recs = pp.getResources(configUrl);
for (int i = 0;i < recs.length;i++) {
parseQueryInfoBeansFromXML(recs[i].getInputStream(), infoMap);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return infoMap;

}

 

第二步循环迭代

 

 

/**
* @Description: 解析配置文件,读取相关的查询信息
*
* @param is:
* 查询配置文件输入流
* @param map:
* 查询配置信息
*/
private static void parseQueryInfoBeansFromXML(InputStream is, HashMap map) {
try {
SAXReader builder = new SAXReader();
Document doc = builder.read(is);
List queryList = doc.getRootElement().selectNodes("query");
Iterator queryIt = queryList.iterator();
while (queryIt.hasNext()) {
Element queryE = (Element) queryIt.next();
QueryInfoBean bean = getQueryInfoBeanByNode(queryE);
if (bean!=null){
map.put(bean.getQueryID(), bean);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

 

第三步循环解析

 

/**
* @Description: 解析query 节点,取得一查询信息
* @param queryElement: query element
* @return QueryInfoBean
*/
private static QueryInfoBean getQueryInfoBeanByNode(Element queryElement) {
QueryInfoBean bean = null;
try{
String id = queryElement.attributeValue("busiId");

String queryDesc = null;
Element queryDescE = (Element) queryElement.selectSingleNode("query-desc");

if (queryDescE!=null){
queryDesc = queryDescE.getTextTrim();
}
if (queryDesc==null){
queryDesc = "";
}
Element sqlE = (Element) queryElement.selectSingleNode("sql");
String sql = sqlE.getText();

String orderby = null;
Node orderbyE = queryElement.selectSingleNode("orderby");
if (orderbyE!=null){
orderby = orderbyE.getText();
}
if (orderby==null)
orderby = "";
HashMap paramMap = new HashMap();
List paramList = queryElement.selectNodes("params/param");
if (paramList!=null){
Iterator paramIt = paramList.iterator();
while (paramIt.hasNext()) {
Element paramE = (Element) paramIt.next();
String name = paramE.attributeValue("name");
String where = paramE.attributeValue("value");
String value = paramE.getText();
if(where!=null){
Map whereMap=null;
if(paramMap.containsKey(name)){
Object val=paramMap.get(name);
if(Map.class.isAssignableFrom(val.getClass())){
whereMap=(Map)val;
}else{
whereMap=new HashMap();
whereMap.put(name,val);
}
}
if(whereMap==null) whereMap=new HashMap();
whereMap.put(where, value);
paramMap.put(name, whereMap);
}else{
paramMap.put(name, value);
}
}
}

List columnList = new ArrayList();
List tmpColumnList = queryElement.selectNodes("columns/column");

if (tmpColumnList!=null){
Iterator columnIt = tmpColumnList.iterator();
while (columnIt.hasNext()) {
Element columnE = (Element) columnIt.next();
String columnName = columnE.attributeValue("name");

if(columnName!=null){
ColumnInfoBean columnInfoBean = new ColumnInfoBean();
columnInfoBean.setName(columnName);
columnInfoBean.setShow(true);
columnInfoBean.setExport(true);

String isShow = columnE.attributeValue("show");
String isExport = columnE.attributeValue("export");
String prefix = columnE.attributeValue("prefix");
String align = columnE.attributeValue("align");
String type = columnE.attributeValue("type");
if (align==null){
align = "center";

}

if (type==null){
type = "string";

}

columnInfoBean.setAlign(align);
columnInfoBean.setType(type);

if (prefix==null){
prefix = "";
}
columnInfoBean.setPrefix(prefix);
if (isShow!=null && "0".equals(isShow)){
columnInfoBean.setShow(false);
}
if (isExport!=null && "0".equals(isExport)){
columnInfoBean.setExport(false);
}

columnInfoBean.setTitle(columnE.getText());
columnList.add(columnInfoBean);
}
}
}
// bean 是一个vo对象
bean = new QueryInfoBean();
bean.setQueryID(id);
bean.setQueryDescription(queryDesc);
bean.setQueryScript(sql);
bean.setOrderByScript(orderby);
bean.setConditionsMap(paramMap);
bean.setColumnList(columnList);
}catch(Exception e){
}
return bean;

}

 

main方法测试

 

public static void main(String[] args) {
    getQueryInfoBeanInfo();
}

 

 

此处是xml

 

<?xml version="1.0" encoding="UTF-8"?>
<query-config>

<query busiId="searchContractAuditing">
<query-desc>待办理合同初审查询</query-desc>
<sql><![CDATA[

from cs_contract t1
left join crm_credit_application t2 on t1.business_id = t2.id
left join uc_type_dictionary t3 on t2.examine_loan_type_cd = t3.item_code and t3.type_code = 'LOAN_TYPE_CD'
left join uc_type_dictionary t4 on t1.contract_status = t4.item_code and t4.type_code = 'CS_CONTRACT_STATUS_CD'
where 1 = 1 and (t1.contract_status = '5' or t1.contract_status='72')
AND [q_contractNum]
AND [q_cusName]
AND [q_cardId]
AND [q_deptName]
AND [q_loanType]
AND [q_beginTime]
AND [q_endTime]
AND [q_sysUserId]
%%%ORDERBY%%%
]]>
</sql>
<orderby><![CDATA[ order by t1.MODIFY_TIME asc nulls last]]></orderby>
<params>
<param name="q_contractNum"><![CDATA[ t1.contract_number like '%?%' ]]></param>
<param name="q_cusName"><![CDATA[ t2.credit_name like '%?%' ]]></param>
<param name="q_cardId"><![CDATA[ t2.id_number like '%?%' ]]></param>
<param name="q_deptName"><![CDATA[ t2.cus_dept_name in(select tm.organ_name from uc_organ tm where tm.id in (?)) ]]></param>
<param name="q_loanType"><![CDATA[ t2.examine_loan_type_cd = '?' ]]></param>
<param name="q_beginTime"><![CDATA[ t1.real_loan_time >= to_date('?','yyyy-MM-dd')]]></param>
<param name="q_endTime"><![CDATA[ t1.real_loan_time <= to_date('?','yyyy-MM-dd')]]></param>
<param name="q_sysUserId"><![CDATA[ trim(t2.cus_dept_name) in (select trim(c.dept_name) from CS_CONTRACT_PERMISSION c where c.emp_id = ?) ]]></param>
</params>
</query>

</query-config>

 

posted @ 2014-10-09 12:22  也许还年轻  阅读(454)  评论(0编辑  收藏  举报