JavaSE learning notes SAX analysis (6), javasesax

Source: Internet
Author: User

JavaSE learning notes SAX analysis (6), javasesax

Simple Api for Xml is a set of Apis provided by SUN for Simple XML data operations.

The parsing principle adopted by SAX is based on the event-triggered mechanism.

The SAX technology can only read XML data.

1. Prepare the XML file linkmans. xml to be parsed.
<? Xml version = "1.0" encoding = "UTF-8" standalone = "no"?> <Linkmans> <linkman> <name> jack </name> <phone> 18663243245 </phone> <email> jack@163.com </email> </linkman> <name> zhang San </name> <phone> 1353243247 </phone> <email> zs@126.com </email> </linkman> </linkmans>
2. get the SAX Parser // 2. get the SAX Parser public static SAXParser getParser () throws Exception {// 2.1 get the parser factory class Object SAXParserFactory factory = SAXParserFactory. newInstance (); SAXParser parser = factory. newSAXParser (); return parser ;}
3. parse data // 3. parse public static void getElement (File file, DefaultHandler dh) throws Exception {// 3.1 get the parser SAXParser parser = getParser (); // 3.2 call the parsing method parser. parse (file, dh );}
4. create a MyHandler inherited from the DefaultHandler class. javapublic class MyHandler extends DefaultHandler {// automatically execute public void startDocument () throws SAXException {super. startDocument (); System. out. println ("XML document starts parsing... ");} // automatically execute public void startElement (String uri, String localName, String name, Attributes attributes) throws SAXException {super. startElement (uri, localName, name, attributes); System. out. println ("element" + name + "start... ");} // automatically execute public void characters (char [] ch, int start, int length) throws SAXException {super. characters (ch, start, length); System. out. println ("text content" + new String (ch, start, length);} // when an element ends, automatically execute public void endElement (String uri, String localName, String name) throws SAXException {super. endElement (uri, localName, name); System. out. println ("element" + name + "ended... ");} // custom execution of public void endDocument () throws SAXException {super. endDocument (); System. out. println ("XML document parsing ends... ");}}
5. the main function calls the method of parsing elements public static void main (String [] args) throws Exception {File file = new File ("linkmans. xml "); MyHandler handler = new MyHandler (); getElement (file, handler );}

Through the above program, it is found that the developer only needs to pass the corresponding event processor object (inherited from the DefaultHandler class) when parsing the SAX ).

Example 1: Create an event processor to obtain the specified node data in XML. Public class OptionDataHandler extends DefaultHandler {// defines the name of a member variable record tag private String name = null; // defines the variable private int count = 0 for a statistic count; // when a starting element is automatically executed, public void startElement (String uri, String localName, String name, Attributes attributes) throws SAXException {super. startElement (uri, localName, name, attributes); this. name = name; if ("name ". equals (this. name) {this. count ++ ;}} // automatically execute public void characters (char [] ch, int start, int length) throws SAXException {super. characters (ch, start, length); // judge whether the element is name if ("name ". equals (this. name) & this. count = 2) {System. out. println (new String (ch, start, length ));}}}
Case 1: use SAX to parse XML data and encapsulate data sets

In fact, using any XML parsing technology to parse XML data requires subsequent data encapsulation. It does not have any meaning to obtain the value of a node separately. Therefore, the XML data must be parsed and encapsulated into the specified collection as an object to facilitate processing by other applications.

1. implement a Linkman. java javabean object public class Linkman {private String name; private String phone; private String email; public Linkman () {super ();} public Linkman (String name, String phone, string email) {super (); this. name = name; this. phone = phone; this. email = email;} // The get and set methods are omitted}
3. create an event processor Listhandler for encapsulation. javapublic class ListHandler extends DefaultHandler {// defines the javabean object private Linkman linkman = null; // defines a set to encapsulate the private List of javabean objects <Linkman> list = null; // define the name of the parsed element private String name = null; @ Override public void startDocument () throws SAXException {super. startDocument (); // create the object of the list set this. list = new ArrayList <Linkman> () ;}@ Override public void startElement (String uri, String localName, String name, Attributes attributes) throws SAXException {super. startElement (uri, localName, name, attributes); // records the name of the parsed element this. name = name; // create a linkman object if ("Linkman ". equals (this. name) {this. linkman = new Linkman () ;}@ Override public void characters (char [] ch, int start, int length) throws SAXException {super. characters (ch, start, length); // if the element name is name, obtain the text value if ("name ". equals (this. name) {this. linkman. setName (new String (ch, start, length);} // if the element name is phone, get the text value if ("phone ". equals (this. name) {this. linkman. setPhone (new String (ch, start, length);} // if the element name is email, obtain the text value if ("email ". equals (this. name) {this. linkman. setEmail (new String (ch, start, length) ;}@ Override public void endElement (String uri, String localName, String name) throws SAXException {super. endElement (uri, localName, name); // release this. name this. name = null; // if the element name is linkman if ("linkman ". equals (name) {// Add the linkman object to the set this. list. add (this. linkman); // clear the linkman object this. linkman = null ;}// provides a public List <linkman> getList () {return list ;}} method for obtaining the encapsulated Linkman set ;}}

Summary: If you use SAX to read data, it must be a good choice. However, this resolution technology cannot perform other operations, such as adding, deleting, and modifying data.

Solution: A technical prize DOM and SAX can be used together to efficiently query and perform actual data operations (add, delete, and modify ). The third-party XML parsing technology DOM4J is used.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.