SAX解析XML

首先,介绍下XML目前的两种写法. 
第1种 
Java代码   收藏代码
  1. <?xml version="1.0"?>  
  2. <user>  
  3.     <customer>  
  4.         <FirstName>Bob2</FirstName>  
  5.         <LastName>Hustead</LastName>  
  6.         <CustId>abc.123</CustId>  
  7.     </customer>  
  8.     <customer>  
  9.         <FirstName>Bob3</FirstName>  
  10.         <LastName>Hustead</LastName>  
  11.         <CustId>abc.123</CustId>  
  12.     </customer>  
  13. </user>  

第2种 
rbac.xml 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <rbac-definition>  
  3.     <organizations>  
  4.         <organization id="1" name="华迪" no="A0"  
  5.             innerno="8790-C9B2-7B08-46FD-A4F8-4157-AD25-5F7B" superiororg="0"  
  6.             virflag="0" status="0" />  
  7.         <organization id="2" name="业务部" no="A1"  
  8.             innerno="0EBE-1B07-BD59-425E-958E-2281-A473-9383" superiororg="1"  
  9.             virflag="0" status="0" />  
  10.         <organization id="3" name="软件中心" no="A2"  
  11.             innerno="D30C-FB7A-E7E-49CE-A3D2-695B-0A4A-CD5B" superiororg="1"  
  12.             virflag="0" status="0" />  
  13.     </organizations>  
  14.     <positions>  
  15.         <position id="1" name="用户" no="A1.1" owner="" />  
  16.         <position id="2" name="主管" no="A1.2" owner="" />  
  17.         <position id="3" name="经理" no="A1.3" owner="" />  
  18.         <position id="4" name="总工" no="A2.1" owner="" />  
  19.     </positions>  
  20.   
  21.     <org-poses>  
  22.         <org-pos orgid="2" posid="1" />  
  23.         <org-pos orgid="2" posid="2" />  
  24.         <org-pos orgid="2" posid="3" />  
  25.         <org-pos orgid="3" posid="4" />  
  26.         <org-pos orgid="3" posid="5" />  
  27.     </org-poses>  
  28. </rbac-definition>  

对于第一种的解析,google上铺天盖地的都是...偏偏兽兽同学要的是第2中的解析..当然也是大同小异..ok..他要求还挺多..还必须把每一个数据项都摘出来..以便进行后续开发使用.那好吧..开工! 

第2步:针对第2中XML以及其中内容进行准备工作 
1.建立org,pos,org_pos的类 便于操作 
Java代码   收藏代码
  1. package parseXML;  
  2.   
  3. public class Organization {  
  4.   
  5.     public int id;  
  6.     public String name;  
  7.     public String no;  
  8.     public String innerno;  
  9.     public int superiororg;  
  10.     public int virflag;  
  11.     public int status;  
  12.   
  13.     public String toString() {  
  14.         String str = "";  
  15.         str = "the org's id is " + id + ",name is " + name + " ,no is " + no  
  16.                 + " ,innerno" + innerno + " ,superiororg is " + superiororg  
  17.                 + " ,virflag is " + virflag + " ,status is " + status;  
  18.         return str;  
  19.     }  
  20. }  
  21.   
  22. class Position {  
  23.   
  24.     private int id;  
  25.     private String name;  
  26.     private String no;  
  27.     private String owner;  
  28.   
  29.     public int getId() {  
  30.         return id;  
  31.     }  
  32.     public void setId(int id) {  
  33.         this.id = id;  
  34.     }  
  35.     public String getName() {  
  36.         return name;  
  37.     }  
  38.     public void setName(String name) {  
  39.         this.name = name;  
  40.     }  
  41.     public String getNo() {  
  42.         return no;  
  43.     }  
  44.     public void setNo(String no) {  
  45.         this.no = no;  
  46.     }  
  47.     public String getOwner() {  
  48.         return owner;  
  49.     }  
  50.     public void setOwner(String owner) {  
  51.         this.owner = owner;  
  52.     }  
  53.   
  54. }  
  55.   
  56. class Org_pos {  
  57.     private int org_id;  
  58.     private int pos_id;  
  59.   
  60.     public int getOrg_id() {  
  61.         return org_id;  
  62.     }  
  63.     public void setOrg_id(int org_id) {  
  64.         this.org_id = org_id;  
  65.     }  
  66.     public int getPos_id() {  
  67.         return pos_id;  
  68.     }  
  69.     public void setPos_id(int pos_id) {  
  70.         this.pos_id = pos_id;  
  71.     }  
  72.   
  73. }  

2.根据SAX原理,写出来handler,用于解析 
ParseOrg.java 
Java代码   收藏代码
  1. package parseXML;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.xml.sax.Attributes;  
  7. import org.xml.sax.SAXException;  
  8. import org.xml.sax.helpers.DefaultHandler;  
  9.   
  10. public class ParseOrg extends DefaultHandler {  
  11.     boolean hasAttribute = false;  
  12.     Attributes attributes = null;  
  13.       
  14.     private List<Organization> orgList=new ArrayList<Organization>();  
  15.     private List<Position> posList=new ArrayList<Position>();  
  16.     private List<Org_pos> org_posList=new ArrayList<Org_pos>();  
  17.       
  18.       
  19.     public void startDocument() throws SAXException {  
  20.         System.out.println("~~begining~~");  
  21.     }  
  22.     public void endDocument() throws SAXException {  
  23.         System.out.println("~~end~~");  
  24.     }  
  25.       
  26.     public void startElement(String uri, String localName, String qName,  
  27.             Attributes attributes) throws SAXException {  
  28.         if (qName.equals("organization")) {  
  29.             System.out.println("********************************");  
  30.             System.out.println("       Organization :");  
  31.             System.out.println("********************************");  
  32.             System.out.println(attributes.getQName(0) + " is : "  
  33.                     + attributes.getValue(0)+"\n"+attributes.getQName(1) + "is : "  
  34.                     + attributes.getValue(1)+"\n"+attributes.getQName(2) + "is : "  
  35.                     + attributes.getValue(2)+"\n"+attributes.getQName(3) + "is : "  
  36.                     + attributes.getValue(3)+"\n"+attributes.getQName(4) + "is : "  
  37.                     + attributes.getValue(4)+"\n"+attributes.getQName(5) + "is : "  
  38.                     + attributes.getValue(5)+"\n"+attributes.getQName(6) + "is : "  
  39.                     + attributes.getValue(6));  
  40.             Organization org=new Organization();  
  41.             org.id=Integer.parseInt( attributes.getValue(0));  
  42.             org.name=attributes.getValue(1);  
  43.             org.no=attributes.getValue(2);  
  44.             org.innerno=attributes.getValue(3);  
  45.             org.superiororg=Integer.parseInt( attributes.getValue(4));  
  46.             org.virflag=Integer.parseInt( attributes.getValue(5));  
  47.             org.status=Integer.parseInt( attributes.getValue(6));  
  48. //          System.out.println(org.toString());  
  49.             orgList.add(org);  
  50.               
  51.         }  
  52.         if (attributes.getLength() > 0) {  
  53.             System.out.println(attributes.getLength()+"  :attributes length");  
  54.             this.attributes = attributes;  
  55.             this.hasAttribute = true;  
  56.         }  
  57.         if (qName.equals("position")) {  
  58.             System.out.println("********************************");  
  59.             System.out.println("         position :");  
  60.             System.out.println("********************************");  
  61.             System.out.println(attributes.getQName(0) + " is : "  
  62.                     + attributes.getValue(0)+"\n"+attributes.getQName(1) + "is : "  
  63.                     + attributes.getValue(1)+"\n"+attributes.getQName(2) + "is : "  
  64.                     + attributes.getValue(2)+"\n"+attributes.getQName(3) + "is : "  
  65.                     + attributes.getValue(3));  
  66.             Position pos=new Position();  
  67.             pos.setId(Integer.parseInt( attributes.getValue(0)));  
  68.             pos.setName(attributes.getValue(1));  
  69.             pos.setNo(( attributes.getValue(2)));  
  70.             pos.setOwner(attributes.getValue(3));  
  71.             posList.add(pos);  
  72.         }  
  73.         if (qName.equals("org-pos")) {  
  74.             System.out.println("********************************");  
  75.             System.out.println("         org-pose :");  
  76.             System.out.println("********************************");  
  77.             System.out.println(attributes.getQName(0) + " is : "  
  78.                     + attributes.getValue(0)+"\n"+attributes.getQName(1) + "is : "  
  79.                     + attributes.getValue(1));  
  80.             Org_pos org_pos=new Org_pos();  
  81.             org_pos.setOrg_id(Integer.parseInt(attributes.getValue(0)));  
  82.             org_pos.setPos_id(Integer.parseInt(attributes.getValue(1)));  
  83.             org_posList.add(org_pos);  
  84.               
  85.         }  
  86.     }  
  87.   
  88.     public void endElement(String uri, String localName, String qName)  
  89.             throws SAXException {  
  90. //      if (hasAttribute && (attributes != null)) {  
  91. //          for (int i = 0; i < attributes.getLength(); i++) {  
  92. //              System.out.println(attributes.getQName(0)  
  93. //                      + " and its value is : " + attributes.getValue(0));  
  94. //          }  
  95. //      }  
  96.     }  
  97.   
  98. //  public void characters(char[] ch, int start, int length)  
  99. //          throws SAXException {  
  100. //      System.out.println((new String(ch, start, length))+"  :here");  
  101. //  }  
  102.     public List<Position> getPosList() {  
  103.         return posList;  
  104.     }  
  105.     public void setPosList(List<Position> posList) {  
  106.         this.posList = posList;  
  107.     }  
  108.     public List<Org_pos> getOrg_posList() {  
  109.         return org_posList;  
  110.     }  
  111.     public void setOrg_posList(List<Org_pos> org_posList) {  
  112.         this.org_posList = org_posList;  
  113.     }  
  114.     public List<Organization> getOrgList() {  
  115.         return orgList;  
  116.     }  
  117.     public void setOrgList(List<Organization> orgList) {  
  118.         this.orgList = orgList;  
  119.     }  
  120. }  


最后,写出来程序入口即可 
SaxParse.java 
Java代码   收藏代码
  1. package parseXML;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import javax.xml.parsers.ParserConfigurationException;  
  11. import javax.xml.parsers.SAXParser;  
  12. import javax.xml.parsers.SAXParserFactory;  
  13. import org.xml.sax.SAXException;  
  14.   
  15. public class SaxParse {  
  16.   
  17.     /** 
  18.      * 采用SAX解析 
  19.      */  
  20.     public SaxParse() {  
  21.         SAXParserFactory saxfac = SAXParserFactory.newInstance();  
  22.         try {  
  23.             SAXParser saxparser = saxfac.newSAXParser();  
  24.             InputStream is = new FileInputStream(  
  25.                     "C:\\workspace\\Lab\\src\\parseXML\\rbac.xml");  
  26.             ParseOrg parseOrg=new ParseOrg();  
  27.             saxparser.parse(is, parseOrg);//重点  
  28.               
  29.             /** 
  30.              * 全部存到list里了 
  31.              */  
  32.             List<Organization> orgList=new ArrayList<Organization>();  
  33.             orgList=parseOrg.getOrgList();  
  34.             for(int i=0;i<orgList.size();i++){  
  35.                 System.out.println(orgList.get(i)+" : orglist");  
  36.             }  
  37.               
  38.             /** 
  39.              * 全部存到list里了 
  40.              */  
  41.             List<Position> posList=new ArrayList<Position>();  
  42.             posList=parseOrg.getPosList();  
  43.             for(int i=0;i<posList.size();i++){  
  44.                 System.out.println(posList.get(i)+" : posList");  
  45.             }  
  46.               
  47.             /** 
  48.              * 全部存到list里了 
  49.              */  
  50.             List<Org_pos> org_posList=new ArrayList<Org_pos>();  
  51.             org_posList=parseOrg.getOrg_posList();  
  52.             for(int i=0;i<org_posList.size();i++){  
  53.                 System.out.println(org_posList.get(i)+" : org_posList");  
  54.             }  
  55.         } catch (ParserConfigurationException e) {  
  56.             e.printStackTrace();  
  57.         } catch (SAXException e) {  
  58.             e.printStackTrace();  
  59.         } catch (FileNotFoundException e) {  
  60.             e.printStackTrace();  
  61.         } catch (IOException e) {  
  62.             e.printStackTrace();  
  63.         }  
  64.     }  
  65.   
  66.     public static void main(String[] args) {  
  67.         new SaxParse();  
  68.     }  
  69.   
  70. }  


总结:功能已经实现...输出也很漂亮...当然只针对Organization进行了处理..剩下两个的输出样式不再啰嗦了.. 

输出结果: 
Java代码   收藏代码
  1. ********************************  
  2.        Organization :  
  3. ********************************  
  4. id is : 1  
  5. nameis : 华迪  
  6. nois : A0  
  7. innernois : 8790-C9B2-7B08-46FD-A4F8-4157-AD25-5F7B  
  8. superiororgis : 0  
  9. virflagis : 0  
  10. statusis : 0  
  11. 7  :attributes length  
  12. ......  
  13. ********************************  
  14.          position :  
  15. ********************************  
  16. id is : 1  
  17. nameis : 用户  
  18. nois : A1.1  
  19. owneris :   
  20. 4  :attributes length  
  21. .....  
  22. ********************************  
  23.          org-pose :  
  24. ********************************  
  25. orgid is : 2  
  26. posidis : 1  
  27. 2  :attributes length  

上述结果省略了很多... 
具体效果还是执行一下程序,从控制台观看吧...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值