SAX解析方式小示例

<?xml version="1.0" encoding="utf-8"?>
<country name="中国">
 <province name="吉林省">
  <city>
   长春
        </city>
  <city>
   吉林
        </city>
  <city>
   四平
        </city>
  <city>
   松原
        </city>
  <city>
   通化
        </city>
 </province>
 <province name="山东省">
  <city>
   济南
        </city>
  <city>
   青岛
        </city>
  <city>
   威海
        </city>
  <city>
   烟台
        </city>
 </province>
</country>

 

 

public Map getMes() throws DocumentException {
  HashMap<String, List> info = new HashMap<String, List>();
  SAXReader reader = new SAXReader();// 获取解析对象
  Document document = reader.read("src/cn/it/db/province.xml");

  Element root = document.getRootElement();// 得到根节点
  List provinces = root.elements();// 得到字节点集合

  for (int i = 0; i < provinces.size(); i++) {
   List<String> cityVal = new ArrayList<String>();
   Element province = (Element) provinces.get(i);
   String provinceVal = province.attributeValue("name");// 获取省名称

   List cities = province.elements();
   for (int j = 0; j < cities.size(); j++) {
    Element city = (Element) cities.get(j);// 获取城市节点
    cityVal.add(city.getTextTrim());
   }

   info.put(provinceVal, cityVal);
  }
  return info;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值