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

public class SaxUtil {
   
    //供调用的方法
    public static String getMyNodeValue(String filepath, String xpath) throws SAXException, IOException, ParserConfigurationException {
               
        SAXParserFactory sf = SAXParserFactory.newInstance();// 通过工厂获得SAX解析器
       
        SAXParser sax = sf.newSAXParser();//解析器
       
        SAXHander sh = new SaxUtil().new SAXHander(xpath);// 通过解析器解析xml文件
       
        sax.parse(filepath, sh); //使用自定义的监听器
       
        return sh.result;
    }
   
    //test main
    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
//          String  xpath = "Manifest/Declaration/BorderTransportMeans/JourneyID";
//            String filepath = "c:/CN_MT1101_1p0_COSCON_20090504130040082.xml";

          String xpath = "NS1:CUSTXML/NS1:HEAD/NS1:FILLER2";
          String  filepath = "c:/O_F096F6986F80D711340DF0B4B082D396";
         
          getMyNodeValue(filepath, xpath);
    }

    //自定义sax解析监听器
    class SAXHander extends DefaultHandler {
        private String mypath = null;
        private String[] pps = null;
        private int i = 0;
        private String result = null;//结果

        public SAXHander(String path) {
            super();
            this.mypath = path;
            init();
        }
        private void init() {
            if (mypath != null && !"".equals(mypath.trim())) {
                pps = mypath.split("/");
            }
        }
        public void startDocument() throws SAXException {
            //System.out.println("文档开始 ");
        }

        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            //System.out.println("元素开始 :" + qName);

            if (pps != null && pps.length > 0) {
                for (int j = 0; j < pps.length; j++) {
                    if (qName.equals(pps[j])) {
                        if (i == j)
                            i++;
                    }
                }
            }
        }
        public void characters(char[] ch, int start, int length) throws SAXException {
            String text = new String(ch, start, length);
            //去掉xml文件中的空格节点
            if (text.trim().equals("")) {
                return;
            }

            if (pps != null && i == pps.length) {
                result = text;
                System.out.println("文本内容 :" + text);
                i = -1;
            }

        }

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值