首先,介绍下XML目前的两种写法.
第1种
第2种
rbac.xml
对于第一种的解析,google上铺天盖地的都是...偏偏兽兽同学要的是第2中的解析..当然也是大同小异..ok..他要求还挺多..还必须把每一个数据项都摘出来..以便进行后续开发使用.那好吧..开工!
第2步:针对第2中XML以及其中内容进行准备工作
1.建立org,pos,org_pos的类 便于操作
2.根据SAX原理,写出来handler,用于解析
ParseOrg.java
最后,写出来程序入口即可
SaxParse.java
总结:功能已经实现...输出也很漂亮...当然只针对Organization进行了处理..剩下两个的输出样式不再啰嗦了..
输出结果:
上述结果省略了很多...
具体效果还是执行一下程序,从控制台观看吧...
第1种
- <?xml version="1.0"?>
- <user>
- <customer>
- <FirstName>Bob2</FirstName>
- <LastName>Hustead</LastName>
- <CustId>abc.123</CustId>
- </customer>
- <customer>
- <FirstName>Bob3</FirstName>
- <LastName>Hustead</LastName>
- <CustId>abc.123</CustId>
- </customer>
- </user>
第2种
rbac.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <rbac-definition>
- <organizations>
- <organization id="1" name="华迪" no="A0"
- innerno="8790-C9B2-7B08-46FD-A4F8-4157-AD25-5F7B" superiororg="0"
- virflag="0" status="0" />
- <organization id="2" name="业务部" no="A1"
- innerno="0EBE-1B07-BD59-425E-958E-2281-A473-9383" superiororg="1"
- virflag="0" status="0" />
- <organization id="3" name="软件中心" no="A2"
- innerno="D30C-FB7A-E7E-49CE-A3D2-695B-0A4A-CD5B" superiororg="1"
- virflag="0" status="0" />
- </organizations>
- <positions>
- <position id="1" name="用户" no="A1.1" owner="" />
- <position id="2" name="主管" no="A1.2" owner="" />
- <position id="3" name="经理" no="A1.3" owner="" />
- <position id="4" name="总工" no="A2.1" owner="" />
- </positions>
- <org-poses>
- <org-pos orgid="2" posid="1" />
- <org-pos orgid="2" posid="2" />
- <org-pos orgid="2" posid="3" />
- <org-pos orgid="3" posid="4" />
- <org-pos orgid="3" posid="5" />
- </org-poses>
- </rbac-definition>
对于第一种的解析,google上铺天盖地的都是...偏偏兽兽同学要的是第2中的解析..当然也是大同小异..ok..他要求还挺多..还必须把每一个数据项都摘出来..以便进行后续开发使用.那好吧..开工!
第2步:针对第2中XML以及其中内容进行准备工作
1.建立org,pos,org_pos的类 便于操作
- package parseXML;
- public class Organization {
- public int id;
- public String name;
- public String no;
- public String innerno;
- public int superiororg;
- public int virflag;
- public int status;
- public String toString() {
- String str = "";
- str = "the org's id is " + id + ",name is " + name + " ,no is " + no
- + " ,innerno" + innerno + " ,superiororg is " + superiororg
- + " ,virflag is " + virflag + " ,status is " + status;
- return str;
- }
- }
- class Position {
- private int id;
- private String name;
- private String no;
- private String owner;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getNo() {
- return no;
- }
- public void setNo(String no) {
- this.no = no;
- }
- public String getOwner() {
- return owner;
- }
- public void setOwner(String owner) {
- this.owner = owner;
- }
- }
- class Org_pos {
- private int org_id;
- private int pos_id;
- public int getOrg_id() {
- return org_id;
- }
- public void setOrg_id(int org_id) {
- this.org_id = org_id;
- }
- public int getPos_id() {
- return pos_id;
- }
- public void setPos_id(int pos_id) {
- this.pos_id = pos_id;
- }
- }
2.根据SAX原理,写出来handler,用于解析
ParseOrg.java
- package parseXML;
- import java.util.ArrayList;
- import java.util.List;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- public class ParseOrg extends DefaultHandler {
- boolean hasAttribute = false;
- Attributes attributes = null;
- private List<Organization> orgList=new ArrayList<Organization>();
- private List<Position> posList=new ArrayList<Position>();
- private List<Org_pos> org_posList=new ArrayList<Org_pos>();
- public void startDocument() throws SAXException {
- System.out.println("~~begining~~");
- }
- public void endDocument() throws SAXException {
- System.out.println("~~end~~");
- }
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if (qName.equals("organization")) {
- System.out.println("********************************");
- System.out.println(" Organization :");
- System.out.println("********************************");
- System.out.println(attributes.getQName(0) + " is : "
- + attributes.getValue(0)+"\n"+attributes.getQName(1) + "is : "
- + attributes.getValue(1)+"\n"+attributes.getQName(2) + "is : "
- + attributes.getValue(2)+"\n"+attributes.getQName(3) + "is : "
- + attributes.getValue(3)+"\n"+attributes.getQName(4) + "is : "
- + attributes.getValue(4)+"\n"+attributes.getQName(5) + "is : "
- + attributes.getValue(5)+"\n"+attributes.getQName(6) + "is : "
- + attributes.getValue(6));
- Organization org=new Organization();
- org.id=Integer.parseInt( attributes.getValue(0));
- org.name=attributes.getValue(1);
- org.no=attributes.getValue(2);
- org.innerno=attributes.getValue(3);
- org.superiororg=Integer.parseInt( attributes.getValue(4));
- org.virflag=Integer.parseInt( attributes.getValue(5));
- org.status=Integer.parseInt( attributes.getValue(6));
- // System.out.println(org.toString());
- orgList.add(org);
- }
- if (attributes.getLength() > 0) {
- System.out.println(attributes.getLength()+" :attributes length");
- this.attributes = attributes;
- this.hasAttribute = true;
- }
- if (qName.equals("position")) {
- System.out.println("********************************");
- System.out.println(" position :");
- System.out.println("********************************");
- System.out.println(attributes.getQName(0) + " is : "
- + attributes.getValue(0)+"\n"+attributes.getQName(1) + "is : "
- + attributes.getValue(1)+"\n"+attributes.getQName(2) + "is : "
- + attributes.getValue(2)+"\n"+attributes.getQName(3) + "is : "
- + attributes.getValue(3));
- Position pos=new Position();
- pos.setId(Integer.parseInt( attributes.getValue(0)));
- pos.setName(attributes.getValue(1));
- pos.setNo(( attributes.getValue(2)));
- pos.setOwner(attributes.getValue(3));
- posList.add(pos);
- }
- if (qName.equals("org-pos")) {
- System.out.println("********************************");
- System.out.println(" org-pose :");
- System.out.println("********************************");
- System.out.println(attributes.getQName(0) + " is : "
- + attributes.getValue(0)+"\n"+attributes.getQName(1) + "is : "
- + attributes.getValue(1));
- Org_pos org_pos=new Org_pos();
- org_pos.setOrg_id(Integer.parseInt(attributes.getValue(0)));
- org_pos.setPos_id(Integer.parseInt(attributes.getValue(1)));
- org_posList.add(org_pos);
- }
- }
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- // if (hasAttribute && (attributes != null)) {
- // for (int i = 0; i < attributes.getLength(); i++) {
- // System.out.println(attributes.getQName(0)
- // + " and its value is : " + attributes.getValue(0));
- // }
- // }
- }
- // public void characters(char[] ch, int start, int length)
- // throws SAXException {
- // System.out.println((new String(ch, start, length))+" :here");
- // }
- public List<Position> getPosList() {
- return posList;
- }
- public void setPosList(List<Position> posList) {
- this.posList = posList;
- }
- public List<Org_pos> getOrg_posList() {
- return org_posList;
- }
- public void setOrg_posList(List<Org_pos> org_posList) {
- this.org_posList = org_posList;
- }
- public List<Organization> getOrgList() {
- return orgList;
- }
- public void setOrgList(List<Organization> orgList) {
- this.orgList = orgList;
- }
- }
最后,写出来程序入口即可
SaxParse.java
- package parseXML;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- import org.xml.sax.SAXException;
- public class SaxParse {
- /**
- * 采用SAX解析
- */
- public SaxParse() {
- SAXParserFactory saxfac = SAXParserFactory.newInstance();
- try {
- SAXParser saxparser = saxfac.newSAXParser();
- InputStream is = new FileInputStream(
- "C:\\workspace\\Lab\\src\\parseXML\\rbac.xml");
- ParseOrg parseOrg=new ParseOrg();
- saxparser.parse(is, parseOrg);//重点
- /**
- * 全部存到list里了
- */
- List<Organization> orgList=new ArrayList<Organization>();
- orgList=parseOrg.getOrgList();
- for(int i=0;i<orgList.size();i++){
- System.out.println(orgList.get(i)+" : orglist");
- }
- /**
- * 全部存到list里了
- */
- List<Position> posList=new ArrayList<Position>();
- posList=parseOrg.getPosList();
- for(int i=0;i<posList.size();i++){
- System.out.println(posList.get(i)+" : posList");
- }
- /**
- * 全部存到list里了
- */
- List<Org_pos> org_posList=new ArrayList<Org_pos>();
- org_posList=parseOrg.getOrg_posList();
- for(int i=0;i<org_posList.size();i++){
- System.out.println(org_posList.get(i)+" : org_posList");
- }
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- new SaxParse();
- }
- }
总结:功能已经实现...输出也很漂亮...当然只针对Organization进行了处理..剩下两个的输出样式不再啰嗦了..
输出结果:
- ********************************
- Organization :
- ********************************
- id is : 1
- nameis : 华迪
- nois : A0
- innernois : 8790-C9B2-7B08-46FD-A4F8-4157-AD25-5F7B
- superiororgis : 0
- virflagis : 0
- statusis : 0
- 7 :attributes length
- ......
- ********************************
- position :
- ********************************
- id is : 1
- nameis : 用户
- nois : A1.1
- owneris :
- 4 :attributes length
- .....
- ********************************
- org-pose :
- ********************************
- orgid is : 2
- posidis : 1
- 2 :attributes length
上述结果省略了很多...
具体效果还是执行一下程序,从控制台观看吧...