用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
    评论
要使用 SAX 解析器获 XML 节点的子节点,可以在 DefaultHandler 类的子类中重写 startElement() 方法,处理 XML 文件中的开始标签,并在其中获节点。在 startElement() 方法中,可以使用 Attributes 对象获当前开始标签的属性,并使用 XMLReader 对象获当前节点的子节点。下面是一个示例代码,演示如何使用 SAX 解析器获 XML 节点的子节点: ```java import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; public class SAXParserExample extends DefaultHandler { private String currentNode; public static void main(String[] args) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); SAXParserExample handler = new SAXParserExample(); parser.parse("example.xml", handler); } public void startElement(String uri, String localName, String qName, Attributes attributes) { currentNode = qName; if (qName.equals("book")) { String id = attributes.getValue("id"); System.out.println("Book ID: " + id); } } public void characters(char[] ch, int start, int length) { String text = new String(ch, start, length); if (currentNode.equals("title")) { System.out.println("Book Title: " + text); } if (currentNode.equals("author")) { System.out.println("Book Author: " + text); } } } ``` 在上面的代码中,我们重写了 DefaultHandler 类的 startElement() 方法和 characters() 方法。在 startElement() 方法中,我们使用 currentNode 变量记录当前节点的名称,如果当前节点是 book 节点,则使用 Attributes 对象获 book 节点的 id 属性。在 characters() 方法中,我们通过判断 currentNode 变量的,获 book 节点的 title 和 author 子节点的文本内容。注意,在 startElement() 方法中,我们不能直接获节点的文本内容,因为子节点的文本内容可能会被分为多个片段,分别在 characters() 方法中处理。因此,我们需要使用一个变量来记录当前节点的名称,在 characters() 方法中根据节点名称来处理文本内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值