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.