XML tutorial (2)
Author: I am a Chu madman
Download source code
I. Sax Architecture
Sax contains multiple handler processors, many of which process events when processing XML resources.
Contenthandler is the most important one. It is used to parse the XML document, while dtdhandler verifies the DTD file.
Msxml3 currently includes the following processors:
- Imxattributes Interface
- Imxwriter Interface
- Isaxattributes Interface
- Isaxcontenthandler Interface
- Isaxdeclhandler Interface
- Isaxdtdhandler Interface
- Isaxentityresolver Interface
- Isaxerrorhandler Interface
- Isaxlexicalhandler Interface
- Isaxlocator Interface
- Isaxxmlfilter Interface
- Isaxxmlreader Interface
Ii. isaxcontenthandler Interface
Contenthandler has the following functions:
Characters |
Enddocument |
Startdocument |
Endelement |
Startelement |
Ignorablewhitespace |
Startprefixmapping |
Processinginstruction |
Skippedentity |
|
The functions to be explained today are characters, startdocument/enddoucment, and startelement/endelement functions. Their usage:
Iii. Function Description
Hresult characters ([in] const wchar_t * pwchchars, [in] int cchchars); pwchchars gets string information. Cchchars gets the length of the string.
Hresult startdocument (); this function indicates that the document is parsed.
Hresult startelement ([in] const wchar_t * pwchnamespaceuri, [in] int cchnamespaceuri, [in] const wchar_t * pwchlocalname, [in] int cchlocalname [in] const wchar_t * [in] int cchqname); [in] isaxattributes * pattributes); pwchnamespaceuri gets the namespace URI. Cchnamespaceuri gets the URI length. Pwchlocalname gets the tag. Cchlocalname gets the tag length. Obtain the QNAME from pwchqname. The length of the QNAME obtained by cchqname. Pattributes obtains the attribute information of an element.
Because the element attributes are not fixed, you need to obtain them using the following method. Pattributes-> getlength (& L); For (INT I = 0; I <L; I ++) {wchar_t * ln, * VL; int lnl, Vll; pattributes-> getlocalname (I, & ln, & lnl); PRT (L "% s =", LN, lnl); pattributes-> getvalue (I, & VL, & VLL); PRT (L "/" % S/"", VL, VLL );}
4. Use contenthandler:
Hero_xmlcontenthandler * phero_xml; isaxxmlreaderptr preader = NULL; hresult hr; CHR (preader. createinstance (_ uuidof (saxxmlreader); // only one content handler can be registered at a time. phero_xml = hero_xmlcontenthandler: createinstance (); // generate the hero_xmlcontenthandler object. CHR (preader-> putcontenthandler (phero_xml); // register the hero_xmlcontenthandler processor. CHR (preader-> parseurl (L "Hero. xml"); // resolution path. CHR (preader-> putcontenthandler (null ));
Program running diagram.
V. Postscript
I hope this article will help you. You are welcome to criticize and correct it. For more information, see the source code. The Development Kit is msxml3, and the principles of Apache xerces are also the same.