Java dom4j Parsing xml

Source: Internet
Author: User
Tags gettext

The first step gets the path

/**
* @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 file in Project path
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;

}

The second iteration of the loop

/**
* @Description: Parse the configuration file, read the relevant query information
*
* @param is:
* Query configuration file input stream
* @param map:
* Query configuration information
*/
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 ();
}
}

Third-Step loop parsing

/**
* @Description: Parse the query node to get a search information
* @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 by = null;
Node Orderbye = Queryelement.selectsinglenode ("by");
if (orderbye!=null) {
= Orderbye.gettext ();
}
if (orderby==null)
= "";
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 is a Vo object
Bean = new Queryinfobean ();
Bean.setqueryid (ID);
Bean.setquerydescription (QUERYDESC);
Bean.setqueryscript (SQL);
Bean.setorderbyscript (by the above);
Bean.setconditionsmap (PARAMMAP);
Bean.setcolumnlist (columnlist);
}catch (Exception e) {
}
return bean;

}

Main Method Test

public static void Main (string[] args) {
Getqueryinfobeaninfo ();
}

Here is the XML

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

<query busiid= "Searchcontractauditing" >
<query-desc> pending contract preliminary inquiry </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>

Java dom4j Parsing xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.