How to parse Xml file -- SAX!

Posted pp_crz_coder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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?

以上是关于How to parse Xml file -- SAX!的主要内容,如果未能解决你的问题,请参考以下文章

symbian s60 上的 XML 解析错误

jackson 序列化忽略未知字段: How to Ignore Unknown Properties While Parsing JSON in Java

[同步电子邮件数据库]如何将电子邮件解析到数据库?(How to parse e-mail into database?)

How can I add files to a Jar file?

How to install .deb file in Ubuntu

How to find configuration file MySQL uses?(转)