码迷,mamicode.com
首页 > 其他好文 > 详细

How to parse Xml file -- SAX!

时间:2017-07-12 13:50:44      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:bean   string   star   cep   and   tar   ase   qname   cas   

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?

How to parse Xml file -- SAX!

标签:bean   string   star   cep   and   tar   ase   qname   cas   

原文地址:http://www.cnblogs.com/ppcoder/p/7154739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!