浏览 3226 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-06-12
<?xml version="1.0" encoding="GBK"?> <china> <province name="直辖市"> <city>北京</city> <city>上海</city> <city>天津</city> <city>重庆</city> </province> <province name="广东"> <city>广州</city> <city>珠海</city> <city>深圳</city> <city>东莞</city> </province> <province name="广西"> <city>桂林</city> <city>柳州</city> <city>北海</city> <city>南宁</city> </province> <province name="海南"> <city>海口</city> <city>三亚</city> </province> <province name="湖北"> <city>武汉</city> <city>鄂州</city> <city>荆州</city> <city>十堰</city> </province> <province name="湖南"> <city>长沙</city> <city>岳阳</city> <city>常德</city> <city>张家界</city> </province> <province name="浙江"> <city>杭州</city> <city>绍兴</city> <city>宁波</city> <city>台州</city> </province> <province name="辽宁"> <city>沈阳</city> <city>大连</city> <city>抚顺</city> <city>铁岭</city> </province> </china> 然后写了一个Java类,用来读取省份和城市/ package com.selectdemo.struts.action; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; public class ReadXml { Element root = null; public ReadXml() throws FileNotFoundException, JDOMException, IOException { super(); SAXBuilder sb = new SAXBuilder();//解析器对象 Document doc = sb.build(new FileInputStream("src/com/selectdemo/struts/action/city.xml"));//绑定文件 root = doc.getRootElement();//获取根元素 } public List getProvince(){//获取省份 ArrayList provinceList = new ArrayList(); List tempList = root.getChildren();//获取所有省份节点 for(int i=0; i<tempList.size();i++){ Element province=(Element)tempList.get(i);//子节点转型 provinceList.add(province.getAttributeValue("name"));//获取省份节点属性内容 } return provinceList; } public List getCity(String province){ ArrayList cityList = new ArrayList(); List provincetemplist = root.getChildren();//省份集合 for(int i=0; i<provincetemplist.size();i++){ Element provinceElement = (Element)provincetemplist.get(i); if((provinceElement.getAttributeValue("name")).equals(province)){//如果属性为传进来的名称 List cityTempList = provinceElement.getChildren();//获取子节点集合 for(int j=0; j<cityTempList.size(); j++){//循环 Element cityElement = (Element)cityTempList.get(j);//当前城市节点 cityList.add(cityElement.getTextTrim());//增加城市到集合 } } } return cityList; } public static void main(String[] args) throws JDOMException, IOException{ //测试省份 ReadXml rx = new ReadXml(); List provincelist = rx.getProvince(); for(int i=0; i<provincelist.size(); i++){ System.out.println((String)provincelist.get(i)); } //测试城市 List cityList = rx.getCity("直辖市"); for(int i=0; i<cityList.size(); i++){ System.out.println((String)cityList.get(i)); } } } 测试都已经成功,能分别返回省份和城市,经查询可以使用Dwr框架实现二级联动菜单,查了许多资料,但是基于Struts实现的却不多,实在无从下手,请大家给点解题思路或源码以供参考,谢谢大家,如有Struts的实例,如果方便的话,请发至邮箱:zhanghaidang@sina.com,急急 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |