pp_crz_coder

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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?

posted on 2017-07-12 12:02  ppcoder  阅读(149)  评论(0编辑  收藏  举报