zoukankan      html  css  js  c++  java
  • How to parse Xml file -- SAX!

    Different from DOM parser, the SAX parser will parse the file from one node to another.

    There are several methods are common used in SAX parser:

      startDocument()

      startElement()

      character()

      endElement()

      endDocument()

    For example:

      <Books>              ------> startDocument()

        <Book>              ------> startElement()

          <name>           ------> startElement()

            General         ------> character()

          </name>            ------> endELement()

          <price>18$</price>

        </Book>               ------> endELement()

      </Books>               ------> endDocument()

    How to get the SAXParser?

      //1.get the SAXPaserFactory object

      SAXParserFactory factory = SAXParserFactory.newInstance();

      //2.get the SAXParser by SAXParserFactory

      SAXParser parser = factory.newSAXParser();

      //3.use the parser to parse specific xml file

      parser.parse("xml's path",new DefaultHandler{

        public void startElement(String uri,String localName,String qName,Attribute attibute) throws SAXException

          

        }

        public void endElement(String uri,String localName,String qName) throws SAXException{

          

        }

        public void character(char[] chs,int start,int lenght) throws SAXException{

          

        }

      });

    There is an important case: how to write the xml's object to JavaBean?

  • 相关阅读:
    (15)疯狂的程序员----《绝影》
    (14)嵌入式软件开发工程师技能要求总结
    (13)碎片化阅读只会让你变得越来越愚蠢
    (12)QT中搭建opencv开发环境
    (11)git服务器的安装和配置
    (10)python学习笔记一
    (3.3)狄泰软件学院C++课程学习剖析四
    (9)Linux下gdb调试学习
    (8)Linux(客户端)和Windows(服务端)下socket通信实例
    springMVC伪静态
  • 原文地址:https://www.cnblogs.com/ppcoder/p/7154739.html
Copyright © 2011-2022 走看看