例子一:
获取银行的code 和value
bankhf.xml
<?xml version="1.0" encoding="UTF-8"?>
<banktype>
<property>
<key>01020000</key>
<value>中国工商银行</value>
</property>
<property>
<key>01030000</key>
<value>中国农业银行</value>
</property>
</banktype>
package com.sxiic.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import com.sxiic.entity.Bank;
public class BankHfXmlUtil {
private static Map<String, Bank> bankMap = new HashMap<String, Bank>();
// 配置文件名称
private static final String conifgName = "/bankhf.xml";
private static BankHfXmlUtil instance;
private static Document document;
public BankHfXmlUtil() {
SAXBuilder builder = new SAXBuilder();
try {
document = builder.build(getClass().getResourceAsStream(conifgName));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private synchronized static BankHfXmlUtil getInstance() {
if (null == instance) {
instance = new BankHfXmlUtil();
}
return instance;
}
/**
* 获取属性列表
*
* @return
*/
public static Map<String, Bank> getBankMap() {
if (null == instance) {
getInstance();
initBankMap();
}
return bankMap;
}
private static void initBankMap() {
Element root = document.getRootElement();// 获得根节点
List<Element> list = root.getChildren();
// 省份id
for (int i = 0; i < list.size(); i++) {
Element element = list.get(i);
Bank bank = new Bank();
Element key = element.getChild("key");
bank.setId(key.getTextTrim());
//System.out.println(key.getTextTrim());
Element value = element.getChild("value");
bank.setName(value.getTextTrim());
//System.out.println(value.getTextTrim());
bankMap.put(key.getTextTrim(), bank);
}
}
}
public class Bank {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
control.java
BankHfXmlUtil.getBankMap().get(ewvo.getBankType()).getName()
/**
* 获取银行列表
*
* @param model
*/
@RequestMapping(value = "/banklist", method = RequestMethod.GET)
@ResponseBody
public List<Bank> getBanks(Model model) {
List<Bank> bankList = new ArrayList<Bank>();
Map<String, Bank> bankMap = BankHfXmlUtil.getBankMap();
Set<String> bidlist = bankMap.keySet();
List<String> list = new ArrayList<String>(bidlist);
Collections.sort(list); // XML文件中的顺序取出来后,需要排序
for (String bid : list) {
Bank bank = new Bank();
bank.setId(bid);
bank.setName(bankMap.get(bid).getName());
bankList.add(bank);
}
return bankList;
}
bank.js
var getprourl = "select/banklist";
// 获取省份列表,页面加载后立即请求
$(document).ready(
function() {
var hd = $("#btid").val();
$.ajax({
url : getprourl, // 为请求的url
type : "get", // get请求方式
dataType : "json",
success : function(msg) { // 其中msg是服务器上返回的数据,可以alert查看一下
//alert("初始化加载省份信息");
var str = "<option value=''>银行类型</option>";
$.each(msg, function(i, item) {
if (hd == item.id) {
str = str + "<option selected='selected' value='" + item.id + "'>" + item.name + "</option>";
} else {
str = str + "<option value='" + item.id + "'>" + item.name + "</option>";
}
});
$("#bankType").empty();
$("#bankType").append(str);
}
});
});
bank.html
<form:select path="bankType"></form:select>
<input id="btid" name="btid" value="${withdrawals.bankType}" type="hidden" />
---结束
使用列子二:
获取省市,格式如下:
area.xml
<?xml version="1.0" encoding="UTF-8"?>
<dict>
<key>00</key>
<dict>
<key>北京市</key>
<dict>
<key>00</key>
<dict>
<key>北京市</key>
<array>
<string>东城区</string>
<string>西城区</string>
<string>朝阳区</string>
<string>丰台区</string>
<string>石景山区</string>
</array>
</dict>
</dict>
</dict>
<key>01</key>
<dict>
<key>天津市</key>
<dict>
<key>00</key>
<dict>
<key>天津市</key>
<array>
<string>和平区</string>
<string>河东区</string>
<string>河西区</string>
<string>南开区</string>
</array>
</dict>
</dict>
</dict>
</dict>
AreaXmlUtls.java
package com.sxiic.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import com.sxiic.entity.Area;
import com.sxiic.entity.City;
import com.sxiic.entity.Province;
public class AreaXmlUtil {
private static Map<String, Province> areaMap = new HashMap<String, Province>();
private static Map<String, String> provinceMap2 = new HashMap<String, String>();
// 配置文件名称
private static final String conifgName = "/area.xml";
private static AreaXmlUtil instance;
private static Document document;
public AreaXmlUtil() {
SAXBuilder builder = new SAXBuilder();
try {
document = builder.build(getClass().getResourceAsStream(conifgName));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private synchronized static AreaXmlUtil getInstance() {
if (null == instance) {
instance = new AreaXmlUtil();
}
return instance;
}
/**
* 获取省份列表,以及省份下属城市信息
*
* @return
*/
public static Map<String, Province> getAreaMap() {
if (null == instance) {
getInstance();
initAreaMap();
}
return areaMap;
}
private static void initAreaMap() {
Element root = document.getRootElement();// 获得根节点
List<Element> list = root.getChildren();
// 省份id
for (int i = 0; i < list.size(); i++) {
Element element = list.get(i);
if ("key".equals(element.getName())) {
Province province = new Province();
// 省份dist
i = i + 1;
Element dictElement = list.get(i);
List<Element> children = dictElement.getChildren();
for (int j = 0; j < children.size(); j++) {
Element element2 = children.get(j);
// 省份名称
if ("key".equals(element2.getName())) {
province.setId(element.getTextTrim());
province.setName(element2.getTextTrim());
// 省份dist
j = j + 1;
Element element3 = children.get(j);
Map<String, City> cityMap = setCity(element3);
province.setMap(cityMap);
areaMap.put(element.getTextTrim(), province);
}
}
}
}
}
private static Map<String, City> setCity(Element root) {
Map<String, City> cityMap = new HashMap<String, City>();
List<Element> children = root.getChildren();
for (int i = 0; i < children.size(); i++) {
Element element = children.get(i);
// 城市id
if ("key".equals(element.getName())) {
City city = new City();
// 城市dist
i = i + 1;
Element element2 = children.get(i);
List<Element> children2 = element2.getChildren();
for (int j = 0; j < children2.size(); j++) {
Element element3 = children2.get(j);
if ("key".equals(element3.getName())) {
city.setId(element.getTextTrim());
city.setName(element3.getTextTrim());
// 城市dist
j = j + 1;
Element element4 = children2.get(j);
Map<String, Area> areaMap = setArea(element4);
city.setMap(areaMap);
cityMap.put(city.getId(), city);
}
}
}
}
return cityMap;
}
private static Map<String, Area> setArea(Element root) {
Map<String, Area> areaMap = new HashMap<String, Area>();
List<Element> children = root.getChildren();
for (int i = 0; i < children.size(); i++) {
Element element = children.get(i);
// 城市id
if ("string".equals(element.getName())) {
Area area = new Area();
area.setId(String.valueOf(i));
area.setName(element.getTextTrim());
areaMap.put(area.getId(), area);
}
}
return areaMap;
}
---------------以上代码就可以了----------------
/**
* 仅获取省份列表,不获取省份下属城市信息
*
* @return
*/
public static Map<String, String> getProvinceMap2() {
if (null == instance) {
getInstance();
initProvinceMap();
initAreaMap();
}
return provinceMap2;
}
private static void initProvinceMap() {
Element root = document.getRootElement();// 获得根节点
List<Element> list = root.getChildren();
// 省份id
for (int i = 0; i < list.size(); i++) {
Element element = list.get(i);
if ("key".equals(element.getName())) {
// 省份dist
i = i + 1;
Element dictElement = list.get(i);
List<Element> children = dictElement.getChildren();
for (int j = 0; j < children.size(); j++) {
Element element2 = children.get(j);
// 省份名称
if ("key".equals(element2.getName())) {
provinceMap2.put(element.getTextTrim(), element2.getTextTrim());
}
}
}
}
}
public static String getCityName(String provinceId, String cityId) {
return AreaXmlUtil.getAreaMap().get(provinceId).getMap().get(cityId).getName();
}
public static String getProvinceName(String provinceId) {
return AreaXmlUtil.getAreaMap().get(provinceId).getName();
}
public static String getAreaName(String provinceId, String cityId, String areaId) {
return AreaXmlUtil.getAreaMap().get(provinceId).getMap().get(cityId).getMap().get(areaId).getName();
}
public static void main(String[] args) {
//获取省份、城市列表
Map<String, Province> areaMap2 = AreaXmlUtil.getAreaMap();
Set<String> arealist = areaMap2.keySet();
List<String> list = new ArrayList<String>(arealist);
Collections.sort(list);
for (String area : list) {
Province province = areaMap2.get(area);
System.out.print(area+"==> ");
System.out.println(province.getName());
Map<String,City> cityMap = province.getMap();
Set<String> citylist = cityMap.keySet();
List<String> clist = new ArrayList<String>(citylist);
Collections.sort(clist);
for(String carea:clist){
System.out.print(carea+"==> ");
System.out.println(province.getMap().get(carea).getName());
}
}
}
}
-----utils结束---
实体类
package com.sxiic.entity;
import java.util.HashMap;
import java.util.Map;
public class Province {
private String id;
private String name;
private Map<String,City> map = new HashMap<String,City>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, City> getMap() {
return map;
}
public void setMap(Map<String, City> map) {
this.map = map;
}
}
package com.sxiic.entity;
import java.util.HashMap;
import java.util.Map;
public class City {
private String id;
private String name;
private Map<String,Area> map = new HashMap<String,Area>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, Area> getMap() {
return map;
}
public void setMap(Map<String, Area> map) {
this.map = map;
}
}
package com.sxiic.entity;
public class Area {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
---实体类结束--
control中使用
for(EnterpriseWithdrawVO ewvo:list){
if(StringUtils.isNotBlank(ewvo.getCardProvince())){
try {
ewvo.setCardProvinceName(AreaHfXmlUtil.getAreaMap().get(ewvo.getCardProvince()).getName());
} catch (Exception e) {
ewvo.setCardProvinceName("");
}
if(StringUtils.isNotBlank(ewvo.getCardCity())){
try {
ewvo.setCardCityName(AreaHfXmlUtil.getAreaMap().get(ewvo.getCardProvince()).getMap().get(ewvo.getCardCity()).getName());
} catch (Exception e) {
ewvo.setCardCityName("");
}
}
}
}