XML file parsing ------- notes

Source: Internet
Author: User

Using SAX (Simple API
XML) the parsing method scans documents line by line and can stop parsing at any time. However, the operation is complicated and it is difficult to add or delete content to or from a document using its sax. It is both an interface and a software package.

Working principle of sax: Performs sequential scanning on documents. When the scanning starts and ends, elements start and end, an event notification event processing function is generated, and the event processing function performs the corresponding action, then, continue the same scan until the document ends. Document Processing events, element events, DTD or schema events, and error event sax interfaces: contenthandler, errorhandler, dtdhandler, entityresolver (1) Create event processing ProgramThe mycontenthandler class inherits the defaulthandler implementation methods: startdocument, enddocument,
Startelement (string (
Namespace), string (Tag name without prefix), string (Tag name with prefix), attributes (attribute )),
Endelement (string, String, String, attributes), characters (char [] (read within all tags)
Capacity), INT (from which one is read), INT (the length of the read content) (2) create a SAX Parser saxparsefactory = saxparsefactory. newintance (); xmlreader reader = factory. newsaxparse (). getxmlreader (); (3) assigns the event handler to the parser reader. setcontenthandler (New
Mycontenthandler (); // sets the content processor. The various methods in the interface defined in the mycontenthandler class (4) parses the document and sends each event to the handler reader. parse (New inputsourse (New
Stringreader (resultstr); // put the read data directly into the stringreader object. stringreader is used to read the string resultstr as a stream and the files downloaded on the network are stored as strings.

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.