zoukankan      html  css  js  c++  java
  • 使用SAXParser解析XML文档的实例

    使用SAXParser解析时,一定要记得重写方法.

    ------------

    package ParseXML;
    
    import java.io.IOException;
    
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    /**
     * 使用SAXParse解析XML文档
     * @author 小王同学
     *
     */
    public class TestSAXParse {
        public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
            //1.创建SaxParseFactory对象
            SAXParserFactory spf =SAXParserFactory.newInstance();
            //2.创建SAXparser对象(解析器)
            SAXParser parser =spf.newSAXParser();
            //3.创建一个DefultHandler的子类
            BookDefaultHandler bdh =new BookDefaultHandler();
            //4.调用parse方法
            parser.parse("book.xml", bdh);
        }
    
    }
    class BookDefaultHandler extends DefaultHandler {
        //重写第一个方法
        @Override
        public void startDocument() throws SAXException {//解析开始时调用
            System.out.println("解析开始");
            super.startDocument();
        }
        @Override
        public void endDocument() throws SAXException {//解析结束时调用
            System.out.println("解析结束");
            super.endDocument();
        }
        //解析xml文档中的节点时调用
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            // TODO Auto-generated method stub
            super.startElement(uri, localName, qName, attributes);
            if(qName.equals("book")){
                //获取所有的属性
                int count = attributes.getLength();
                for(int i=0;i<count;i++){
                    System.out.println("属性:"+attributes.getQName(i)+"	"+"属性值:"+attributes.getValue(i));
                }
                
            }else if(!"books".equals(qName)&&!"book".equals(qName)){
                System.out.print("节点的名称:"+qName+"	");
            }
        }
        //解析节点结束时调用 
        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            // TODO Auto-generated method stub
            super.endElement(uri, localName, qName);
        }
        //这个方法是用来获取节点的值的方法
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            // TODO Auto-generated method stub
            super.characters(ch, start, length);
            String value =new String(ch,start,length);
            if(!value.trim().equals("")){
                System.out.println("	节点内容:"+value.trim());
            }
            
        }
    }

    ----------------------这里是输入图



  • 相关阅读:
    微软面试问题 情商测试
    SQL游标使用实例
    如何减小与“大牛”的差距
    Dotnet面试题
    排序算法对冒泡排序的优化改进算法
    一个SQL实现薪水大于所在部门平均薪水的员工
    ASP.NET中TextBox设置为Readonly后无法取值的解决办法
    jQuery.Autocomplete实现自动完成功能(详解)
    php发送get、post请求的几种方法
    ISO Latin1字符集
  • 原文地址:https://www.cnblogs.com/xw1024/p/11245310.html
Copyright © 2011-2022 走看看