用SAX取多个节点路径下的值

public class SaxUtil_B {

    public static Map getMyNodeValue(String filepath, List xpaths) {

        SAXHander sh = null;
        try {

            SAXParserFactory sf = SAXParserFactory.newInstance(); // 通过工厂获得SAX解析器

            SAXParser sax = sf.newSAXParser(); //解析器         

            sh = new SaxUtil_B().new SAXHander(xpaths); // 通过解析器解析xml文件

            sax.parse(filepath, sh); //使用自定义的监听器

        } catch (Exception e) {
            e.printStackTrace();
        }
        return sh.m;
    }

    //自定义sax解析监听器
    class SAXHander extends DefaultHandler {         
        private List ipaths = null;
        private String[][] pps = null;     
        private int iii[] = null;
        private int lsize = 0;
         
        private Map m = new HashMap();

        public SAXHander(List paths) {
            super();
            this.ipaths = paths;
            init();
        }
       
        private void init() {

            if (ipaths != null && ipaths.size() > 0) {

                lsize = ipaths.size(); //节点路径数
               
                /** 注意 pps和iii的长度是相同的---lsize(即xpath数)**/
                pps = new String[lsize][]; //节点路径拆分后存储数组
                 
                iii = new int[lsize];
               
               
                for (int i = 0; i < ipaths.size(); i++) {
                    String pp = (String) ipaths.get(i);
                    pps[i] = pp.split("/");
                    //pps_p[i]=pp;
                    iii[i] = 0;
                }

            }

        }
       
        public void startDocument() throws SAXException {
            //开始处理文档
        }

        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

            if (lsize > 0) {

                for (int j = 0; j < pps.length; j++) {
                    if (pps[j] != null && pps[j].length > 0) {

                        for (int jj = 0; jj < pps[j].length; jj++) { //同一path的不同节点
                            if (qName.equals(pps[j][jj])) {

                                if (j <= iii.length && iii[j] == jj) {

                                    iii[j]++;

                                }

                            }
                        }
                    }
                }

            }

        }
       
        public void characters(char[] ch, int start, int length) throws SAXException {
           
            String myvalue = new String(ch, start, length);
            //去掉xml文件中的空格节点
            if (myvalue.trim().equals("")) {
                return;
            }

            if (lsize > 0) {
                for (int i = 0; i < iii.length; i++) {
                    if (iii[i] == pps[i].length) {
                        m.put(ipaths.get(i), myvalue);//get value
                        iii[i] = -1;
                    }
                }
            }             
        }

        public void endElement(String uri, String localName, String qName) throws SAXException {
            // System.out.println("元素结束 :" + qName);
        }
       
        public void endDocument() throws SAXException {
            //文档处理结束
        }
       
    }
}

 

修改后(增加一个供调用方法):


public class SaxUtil {
 
    public SAXParser getSaxParse(){
        SAXParser sax=null;
        try {
             SAXParserFactory sf = SAXParserFactory.newInstance();
             sax = sf.newSAXParser(); // 通过工厂获得SAX解析器
         
           } catch (ParserConfigurationException e) {                 
               e.printStackTrace();
           } catch (SAXException e) {                 
               e.printStackTrace();
           }
           return sax;
    }
   
    public static Map getMyNodeValue(String filepath, List xpaths,SAXParser sax) {

        SAXHander sh = null;
       
        try {           
             
            sh = new SaxUtil().new SAXHander(xpaths);  //使用自定义的监听器
           
            sax.parse(filepath, sh);// 通过解析器解析xml文件
                         
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sh.m;
    }
   
    public static String getRootNodeName(String filepath,SAXParser sax){
       
        SAXHander_Simple sh=null;;
       
        try {
         
            sh = new SaxUtil().new SAXHander_Simple();// 使用自定义的监听器
           
            sax.parse(filepath, sh); // 通过解析器解析xml文件
           
        } catch (SAXException e) {       
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
     
        return sh.RootName;
   }

   class SAXHander_Simple extends DefaultHandler{
       private String RootName=null;
       private int i=0;
      
       public void startDocument() throws SAXException {  }
      
       public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

           if(i==0){
               RootName=qName;           
               i++;
           }
     
       }
      
       public void characters(char[] ch, int start, int length) throws SAXException {  return;  }

       public void endElement(String uri, String localName, String qName) throws SAXException {   return; }
      
       public void endDocument() throws SAXException { }
   }
  
    //自定义sax解析监听器
    class SAXHander extends DefaultHandler {         
        private List ipaths = null;
        private String[][] pps = null;     
        private int iii[] = null;
        private int lsize = 0;
         
        private Map m = new HashMap();

        public SAXHander(List paths) {
            super();
            this.ipaths = paths;
            init();
        }
       
        private void init() {

            if (ipaths != null && ipaths.size() > 0) {

                lsize = ipaths.size(); //节点路径数
               
                /** 注意 pps和iii的长度是相同的---lsize(即xpath数)**/
                pps = new String[lsize][]; //节点路径拆分后存储数组
                 
                iii = new int[lsize];
               
               
                for (int i = 0; i < ipaths.size(); i++) {
                    String pp = (String) ipaths.get(i);
                    pps[i] = pp.split("/");
                    //pps_p[i]=pp;
                    iii[i] = 0;
                }

            }

        }
       
        public void startDocument() throws SAXException { }//开始处理文档

        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

            if (lsize > 0) {

                for (int j = 0; j < pps.length; j++) {
                    if (pps[j] != null && pps[j].length > 0) {

                        for (int jj = 0; jj < pps[j].length; jj++) { //同一path的不同节点
                            if (qName.equals(pps[j][jj])) {

                                if (j <= iii.length && iii[j] == jj) {

                                    iii[j]++;

                                }

                            }
                        }
                    }
                }

            }

        }
       
        public void characters(char[] ch, int start, int length) throws SAXException {
           
            String myvalue = new String(ch, start, length);
            //去掉xml文件中的空格节点
            if (myvalue.trim().equals("")) {
                return;
            }

            if (lsize > 0) {
                for (int i = 0; i < iii.length; i++) {
                    if (iii[i] == pps[i].length) {
                        m.put(ipaths.get(i), myvalue);//get value
                        iii[i] = -1;
                    }
                }
            }             
        }

        public void endElement(String uri, String localName, String qName) throws SAXException { return;}
       
        public void endDocument() throws SAXException {  }//文档处理结束
       
    }
}

public class SaxUtil {
 
    public SAXParser getSaxParse(){
        SAXParser sax=null;
        try {
             SAXParserFactory sf = SAXParserFactory.newInstance();
             sax = sf.newSAXParser(); // 通过工厂获得SAX解析器
         
           } catch (ParserConfigurationException e) {                 
               e.printStackTrace();
           } catch (SAXException e) {                 
               e.printStackTrace();
           }
           return sax;
    }
   
    public static Map getMyNodeValue(String filepath, List xpaths,SAXParser sax) {

        SAXHander sh = null;
       
        try {           
             
            sh = new SaxUtil().new SAXHander(xpaths);  //使用自定义的监听器
           
            sax.parse(filepath, sh);// 通过解析器解析xml文件
                         
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sh.m;
    }
   
    public static String getRootNodeName(String filepath,SAXParser sax){
       
        SAXHander_Simple sh=null;;
       
        try {
         
            sh = new SaxUtil().new SAXHander_Simple();// 使用自定义的监听器
           
            sax.parse(filepath, sh); // 通过解析器解析xml文件
           
        } catch (SAXException e) {       
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
     
        return sh.RootName;
   }

   class SAXHander_Simple extends DefaultHandler{
       private String RootName=null;
       private int i=0;
      
       public void startDocument() throws SAXException {  }
      
       public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

           if(i==0){
               RootName=qName;           
               i++;
           }
     
       }
      
       public void characters(char[] ch, int start, int length) throws SAXException {  return;  }

       public void endElement(String uri, String localName, String qName) throws SAXException {   return; }
      
       public void endDocument() throws SAXException { }
   }
  
    //自定义sax解析监听器
    class SAXHander extends DefaultHandler {         
        private List ipaths = null;
        private String[][] pps = null;     
        private int iii[] = null;
        private int lsize = 0;
         
        private Map m = new HashMap();

        public SAXHander(List paths) {
            super();
            this.ipaths = paths;
            init();
        }
       
        private void init() {

            if (ipaths != null && ipaths.size() > 0) {

                lsize = ipaths.size(); //节点路径数
               
                /** 注意 pps和iii的长度是相同的---lsize(即xpath数)**/
                pps = new String[lsize][]; //节点路径拆分后存储数组
                 
                iii = new int[lsize];
               
               
                for (int i = 0; i < ipaths.size(); i++) {
                    String pp = (String) ipaths.get(i);
                    pps[i] = pp.split("/");
                    //pps_p[i]=pp;
                    iii[i] = 0;
                }

            }

        }
       
        public void startDocument() throws SAXException { }//开始处理文档

        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

            if (lsize > 0) {

                for (int j = 0; j < pps.length; j++) {
                    if (pps[j] != null && pps[j].length > 0) {

                        for (int jj = 0; jj < pps[j].length; jj++) { //同一path的不同节点
                            if (qName.equals(pps[j][jj])) {

                                if (j <= iii.length && iii[j] == jj) {

                                    iii[j]++;

                                }

                            }
                        }
                    }
                }

            }

        }
       
        public void characters(char[] ch, int start, int length) throws SAXException {
           
            String myvalue = new String(ch, start, length);
            //去掉xml文件中的空格节点
            if (myvalue.trim().equals("")) {
                return;
            }

            if (lsize > 0) {
                for (int i = 0; i < iii.length; i++) {
                    if (iii[i] == pps[i].length) {
                        m.put(ipaths.get(i), myvalue);//get value
                        iii[i] = -1;
                    }
                }
            }             
        }

        public void endElement(String uri, String localName, String qName) throws SAXException { return;}
       
        public void endDocument() throws SAXException {  }//文档处理结束
       
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值