Transferred from http://blog.csdn.net/dai_jing/article/details/8393392. The original output is not detailed.
1. Introduction
On the. NET platform, Microsoft provides a wide range of class libraries for C # or hosting C ++ programmers to support various requirements. Among them, there are rich classes for XML file operations. For example, xmldocument and xmlelement. However, the C ++ standard library does not provide the corresponding library. C ++ programmers developed locally generally use open-source class libraries to operate XML files, such as tinyxml, which is superior. Tinyxml is an open-source and free class library that can be used in commercial software for free. Therefore, there are many users. However, maintaining third-party class libraries in projects is sometimes troublesome, so some developers want to avoid using third-party tools. Microsoft provides MSXML to perform operations on XML documents.
2. MSXML and Dom
MSXML is Microsoft XML Core Service. One of the core functions provided by MSXML is to parse XML files and create a DOM tree. You can easily access the content of the DOM tree through interfaces without having to perform memory maintenance on your own. As shown in:
MSXML provides services based on the COM technology and creates MSXML objects through CLSID or progid. Therefore, MSXML requires a basic knowledge base on com. MSXML has multiple versions, and the latest version is 6.0. This article mainly uses version 3.0 to introduce basic usage.
3. common interfaces
Ixmldomdocument represents the entire XML document.
The parent class of various node interfaces of ixmldomnode.
Ixmldomelement represents an element object. Inherited from ixmldomnode
Ixmldomattriement represents the attribute object of an ixmldomelement node, inherited from ixmldomnode
4. Example
# Include "stdafx. H "# include <iostream> # include <objbase. h> # include <msxml2.h> # include <comutil. h> # import "msxml3.dll" using namespace STD; int _ tmain (INT argc, _ tchar * argv []) {// first initialize com hresult hr; hR = coinitialize (null); If (HR! = S_ OK) {cout <"initialize com error. "<Endl; return 0;} // create the Document Object msxml2: ixmldomdocumentptr pdoc; HR = pdoc. createinstance ("msxml2.domdocument. 3.0 "); If (failed (HR) {return 0;} If (false = pdoc-> load (_ bstr_t (" D: \ book. XML ") return 0; // output all content of the XML file cout <" ----------- book. XML -------------- "<Endl; cout <pdoc-> XML <Endl; cout <" --------------------------------- "<Endl; // select the content's root node msxml2 :: ixmldomelementptr pelem = NULL; pelem = pdoc-> selectsinglenode ("catalog"); If (pelem = NULL) return 0; unsigned int nbooknum = pelem-> childnodes-> length; if (nbooknum = 0) return 0; cout <"their are" <nbooknum <"book items in book. XML file. "<Endl; For (INT I = 0; I <nbooknum; I ++) {msxml2: ixmldomnodeptr pbooknode = pelem-> childnodes-> item [I]; if (pbooknode = NULL) return 0; // read the ID attribute msxml2: ixmldomnodeptr pid = pbooknode-> attributes-> getnameditem ("ID") of the book node "); cout <"book id:" <pid-> text <"\ t"; // read the author subnode msxml2 under the book node :: ixmldomnodeptr pauthornode = pbooknode-> selectsinglenode ("author"); If (pauthornode = NULL) {cout <"Author: Error \ t ";} else {cout <"Author:" <pauthornode-> text <"\ t" ;}cout <Endl ;}couninitialize (); Return 0 ;}
Output result:
5. COM smart pointer
In the sample code, we can see that ixmldomelementptr, ixmldomnodeptr, and other smart pointers are used. In msdn, information about ixmldomelementptr cannot be found, but only ixmldomelement, in fact, the suffix with PTR is a smart pointer to the corresponding COM interface. In msxml3.tlh, you can find the following definitions:
_ Com_smartptr_typedef (ixmldomelement, _ uuidof (ixmldomelement ));
_ Com_smartptr_typedef macro is used to define_ Com_ptr_t object, _ com_ptr_tIt encapsulates the COM interface, which is called a smart pointer. This template class is used to allocate and release resources and internally calls QueryInterface, addref, release, and other iunknown functions. This prevents programmers from handling these tedious operations one by one.
After the macro is expanded, the smart pointer ixmldomelementptr is defined, which encapsulates the ixmldomelement interface.