VC uses msxml to parse XML documents, example code

xiaoxiao2021-03-06  41

Now the XML document application is very much special. I have written an example I have written before and have been used later. The first part: DOM parsing: Overview: DOM parses will read a complete XML document to generate a structure tree. This will load all the XML documents to the inner. Therefore, the speed of the parsing will be slower. 1, how to load xml file: // Create DOM, load an XML document MSXML :: IXMLDOMDocumentPtr pCommandDoc; pCommandDoc.CreateInstance (__ uuidof (MSXML :: DOMDocument)); pCommandDoc-> put_async (VARIANT_FALSE); pCommandDoc-> put_validateOnParse (VARIANT_FALSE); pCommandDoc-> put_resolveExternals (VARIANT_FALSE); pCommandDoc-> put_preserveWhiteSpace (VARIANT_TRUE); pCommandDoc-> load (file.GetBuffer (0)); 2, look for the specified node in the XML document: // find MSXML :: IXMLDOMNodePtr pRootNode = pCommandDoc-> selectSingleNode ( "root / record"); if (pRootNode == NULL) {return;} 3, to give the XML document, attributes CString strTemp node; MSXML :: IXMLDOMNamedNodeMapPtr pAttrs = NULL; pRootNode-> get_attributes ( & pAttrs); if (pAttrs == NULL) {return;} MSXML :: IXMLDOMNodePtr pRequestTypeAttr = pAttrs-> getQualifiedItem ( "name", ""); _bstr_t strRequestType = pRequestTypeAttr-> Gettext (); strTemp = strRequestType.operator char * (); 4, get the content of the node _BSTR_T STRVISIPORT = Pnode-> getText (); 5, set the content hr = pnode-> put_text (_BSTR_T (M_STRGATEWAYPASSWORD); 6, set an attribute content ixmldomattribute * pa = null; bstr = sysallocstring (l "attribute 1") PXMLDOM-> CreateAttribute (BSTR, & PNODE); var = variantstring (l "strin"); PA-> PUT_VALUE (var); proot-> setttributenode (pa, & pa1); second part, how to use SAX parsing overview: SAX Used is loaded, it will break the XML document, load it into memory. Use the event notification to represent the node. It seems that there is no ability to write a document.

Its speed is much better than DOM. When using SAX, you need to overload an interface IsaxContentHandler in MSXML4.0. After several functions overloaded, after the node is found, it will call these functions. There are some don't know how to apply for this SAX parsing mode. Just feel that this way may not be very good for that kind of

An analysis of XML documents for structures.

转载请注明原文地址:https://www.9cbs.com/read-71804.html

New Post(0)