SAX解析xml

来源:互联网 发布:js控制div显示隐藏 编辑:程序博客网 时间:2024/06/17 02:56

/**
  *
  * sax解析xml
  */

public static void main(String[] args){
 //创建解析器
 SAXReader xmlReader = new SAXReader();
 //创建集合类型
 Map<String , List<String>> map = new HashMap<String ,List<String>>();
 List<String> list =null;
 try {
  //获取xml路径
  Document doc = xmlReader.read("D:/wwsb.xml");
  //把xml加载到document对象中
  Element myRoot = doc.getRootElement();
  //按照树的思想进行解析
  Element onePerson = myRoot.element("table");
  //得到下一级元素集合
  List<Element> oneElements = onePerson.elements();
  //循环遍历
  
  for (int i = 0; i < oneElements.size(); i++) {
   //获取table的key ,value
   Element e = oneElements.get(i);
  // System.out.println(e);
   //获取cell的key ,value
   List<Element> oneElement2 = e.elements();
   list = new ArrayList<String>();
   for (int j = 0; j < oneElement2.size(); j++) {
    Element e2 = oneElement2.get(j);
    list.add(e2.attributeValue("key"));
    list.add(e2.attributeValue("name"));
    list.add(e2.attributeValue("url"));
    list.add(e2.attributeValue("disable"));
   }
   map.put(e.attributeValue("title"), list);
  }
 } catch (DocumentException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 for (String ss : map.keySet()) {
  //遍历出,cell中的value
  List<String> list2 = map.get(ss);
  System.out.println("打印出cell中的value"+list2);
  for (int i = 0; i < list2.size(); i++) {
  }
 }
}

————————————————————————————————————————————————————————————————————————————————

————————————————————————————————————要解析的的xml类型——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 

<?xml version="1.0" encoding="utf-8"?>

<root>
  <show time_step="" time_unit="">
    <param>
     <replace format="request" name="companyid" place="companyid"/>
     <replace format="request" name="id" place="id"/>
     <replace format="request" name="belongsdate" place="belongsdate"/>
     <replace format="request" name="intact" place="intact"/>
        <replace format="sql"     name="" datasource=""  place="newid"/>
    </param>
  </show>
  <table>
     <row title="、文件审批11">
    <cell  key="card11"  name="办事指南" url="../../syssb/wysb/hpsp/hpsp!guid.action" disable="true"/>
    <cell  key="card12"  name="表格下载" url="../../syssb/wysb/hpsp/hpsp!apply.action"  disable="true"/> 
    <cell  key="card13"  name="在线申报" url="../../syssb/wysb/hpsp/hpsp!regist.action"  disable="true"/>
    <cell key="card14" name="状态查询" url="../../syssb/wysb/hpsp/hpsp!registCon.action" disable="true" /> 
   </row>
   <row title="文件审批21">
    <cell  key="card21"  name="办事指南" url="../../syssb/wysb/hpsp/hpsp!guid.action" disable="true"/>
    <cell  key="card22"  name="表格下载" url="../../syssb/wysb/hpsp/hpsp!apply.action"  disable="true"/>
    <cell  key="card23"  name="在线申报" url="../../syssb/wysb/hpsp/hpsp!regist.action"  disable="true"/>
    <cell  key="card24"  name="状态查询" url="../../syssb/wysb/hpsp/hpsp!registCon.action"  disable="true"/>
   </row>
   <row title="文件审批31">
    <cell  key="card31"  name="办事指南" url="../../syssb/wysb/hpsp/hpsp!guid.action" disable="true"/>
    <cell  key="card32"  name="表格下载" url="../../syssb/wysb/hpsp/hpsp!apply.action"  disable="true"/>
    <cell  key="card33"  name="在线申报" url="../../syssb/wysb/hpsp/hpsp!regist.action"  disable="true"/>
    <cell  key="card34"  name="状态查询" url="../../syssb/wysb/hpsp/hpsp!registCon.action"  disable="true"/>
   </row>
  </table> 
</root>

 

 

0 0