/*** 从XML文件比对,传入provinceId 返回 provinceShortName * @param provinceid* @return*/public static String provinceIdEXprovinceCodeFromXml(String provinceid){ String provinceShortName="";try { SAXReader sax = new SAXReader(); File file=new File(ServletActionContext.getServletContext().getRealPath("/zxdc")+File.separatorChar+"provinceCode.xml");System.out.println("xml路径:"+ServletActionContext.getServletContext().getRealPath("/zxdc/xml")+File.separatorChar+"provinceCode.xml");// File file=new File("E:\\provinceCode.xml"); //测试路径Document xmlDoc = sax.read(file); Element root = xmlDoc.getRootElement();//根节点 Iterator it = root.elementIterator("province"); while(it.hasNext()){ //循环xml子节点Element province = (Element)it.next(); if (provinceid.equals(province.attributeValue("id"))) {//传入参数 provinceId与 子节点id作比对、类查询provinceShortName=province.elementText("sname");//shortName赋值,用于return }} } catch (DocumentException e) { e.printStackTrace(); }return provinceShortName; }
dom4j解析xml文件节点,做类查询操作。
循环比对xml文档中的provinceId ,并返回相应的简拼。
<?xml version="1.0" encoding="UTF-8"?> <provinces><province id="11"><name>北京市</name><sname>BJ</sname></province><province id="12"><name>天津市</name><sname>TJ</sname></province><province id="13"><name>河北省</name><sname>HE</sname></province><province id="14"><name>山西省</name><sname>SX</sname></province><province id="15"><name>内蒙古自治区</name><sname>NM</sname></province><province id="21"><name>辽宁省</name><sname>LN</sname></province><province id="22"><name>吉林省</name><sname>JL</sname></province><province id="23"><name>黑龙江省</name><sname>HL</sname></province><province id="31"><name>上海市</name><sname>SH</sname></province><province id="32"><name>江苏省</name><sname>JS</sname></province><province id="33"><name>浙江省</name><sname>ZJ</sname></province><province id="34"><name>安徽省</name><sname>AH</sname></province><province id="35"><name>福建省</name><sname>FJ</sname></province><province id="36"><name>江西省</name><sname>JX</sname></province><province id="37"><name>山东省</name><sname>SD</sname></province><province id="41"><name>河南省</name><sname>HA</sname></province><province id="42"><name>湖北省</name><sname>HB</sname></province><province id="43"><name>湖南省</name><sname>HN</sname></province><province id="44"><name>广东省</name><sname>GD</sname></province><province id="45"><name>广西壮族自治区</name><sname>GX</sname></province><province id="46"><name>海南省</name><sname>HI</sname></province><province id="50"><name>重庆市</name><sname>CQ</sname></province><province id="51"><name>四川省</name><sname>SC</sname></province><province id="52"><name>贵州省</name><sname>GZ</sname></province><province id="53"><name>云南省</name><sname>YN</sname></province><province id="54"><name>西藏自治区</name><sname>XZ</sname></province><province id="61"><name>陕西省</name><sname>SN</sname></province><province id="62"><name>甘肃省</name><sname>GS</sname></province><province id="63"><name>青海省</name><sname>QH</sname></province><province id="64"><name>宁夏回族自治区</name><sname>NX</sname></province><province id="65"><name>新疆维吾尔自治区</name><sname>XJ</sname></province> </provinces>