Because XML parsing is used recently, I have read some knowledge about XML and found many examples. However, this is not so effective or can be compiled.
Therefore, I modified and tried a copy of the code I copied from the Internet. The problem was fixed in the middle, so I had the following article. The program code can be copied and run directly.
There is basically no need to modify it. If there are some problems, check the introduction of afx. note the H header file, and the problem may be that in the # import "msxml6.dll" section, this dynamic link library file is introduced because msxml6.dll is used by the user, introduce related files according to your own MSXML version.
//////////////////////////////////////// ///////////////////////////////////
# Include "stdafx. H "# include <iostream> // In the Win32 console application, select the last option in the project, open the settings, and select the general option. // on the right, the default option in use of MFC is: use standard Windows libraries // The option is changed to: use MFC in a shared DLL # include <afx. h> // introduce the MSXML parser # import "msxml6.dll" using namespace msxml2; using namespace STD; void createxml (); // create the XML file void parsexml (); // parse the XML file int _ tmain (INT argc, _ tchar * argv []) {createxml (); couninitial Ize (); parsexml (); couninitialize (); System ("pause"); Return 0 ;} /// // create an XML file ////////////////// //////////////////////////////////////// //// // void createxml () {msxml2: ixmldomdocumentptr pdoc; // document class msxml2: ixmldomelementptr xmlroot; // element class, used to store the root element msxml2: ixmldomnodeptr xmlnode; // node class hresult hr; hR = coinitialize (null); If (failed (HR) Throw ("COM Initialization fail"); H R = pdoc. createinstance (_ uuidof (msxml2: domdocument); If (failed (HR) {cout <"the DM object cannot be created! "<Endl ;}// sequence for creating a Prolog handler msxml2: ixmldomprocessinginstructionptr head; head = pdoc-> createprocessinginstruction (_ bstr_t (_ T (" XML ")), _ bstr_t (_ T ("version = '1. 0 'encoding= 'utf-8' "); pdoc-> appendchild (head); // Add the sequence to the document class. // ------------------------- Create the root element ------------------------------- _ variant_t vartyp (short) msxml2: node_element); xmlroot = pdoc-> createelement ("people "); xmlroot-> setattribute ("ID", (_ bstr_t) (_ T ("1-1-1 "))); // m_id is cstring type pdoc-> appendchild (xmlroot); // Add the root element to the document class // --------------------------- create a child element, two methods are used: --------------------- xmlnode = pdoc-> createnode (vartyp, "name", ""); xmlnode-> put_text (_ BS Tr_t (_ T ("SK"); xmlroot-> appendchild (xmlnode); xmlnode = pdoc-> createelement ("compression Sion "); xmlnode-> text = (_ bstr_t) (_ T ("student"); xmlroot-> appendchild (xmlnode); // mongopdoc-> Save ("D: // people. XML "); // Save the XML document // couninitialize ();} /// // parse the XML file ///////////// /// // void parsexml () {msxml2: ixmldomd Ocumentptr pdoc; // XML document class BSTR name, content; // used to save the node name and content msxml2: domnodetype type; // defines the node type variable int I; // ---------------------------- create the document class export hresult hr; HR = coinitialize (null); If (failed (HR) Throw ("COM Initialization fail"); HR = pdoc. createinstance (_ uuidof (msxml2: domdocument); If (failed (HR) {cout <"cannot create a DOM object! "<Endl;} pdoc-> load (" D: // people. XML "); // import the XML document // ---------------------------- get the element pointer of the root node samples // XML document and initialize it to the root node. Note that it is equivalent to database msxml2: ixmldomelementptr proot = NULL; hR = pdoc-> get_documentelement (& proot); If (failed (HR) {cout <"An error occurred while obtaining the root node! "<Endl ;;}// ---------------- test the root node ------------- cstring STR; proot-> get_nodename (& name); STR = Name; proot-> get_text (& content ); STR = content; proot-> get_nodetype (& type); // -------------------- get the root node attribute ----------------- // The Node attribute, put it in the linked list msxml2: Repeated pattrmap = NULL; msxml2 :: ixmldomnodeptr pattritem; proot-> get_attributes (& pattrmap); Long Count; pattrmap-> get_length (& COUNT); // number of attributes in the node Attribute Table pattrmap-> get_item (0, & pattritem); // read the first attribute pattritem-> get_text (& content); // The attribute value STR = content; pattritem-> get_nodename (& name ); // property name STR = Name; // ------------------------------ obtain the subelement list of the root element ------------------------- // obtain the subnode linked list; note that it is equivalent to a table or recordset msxml2 :: ixmldomnodelistptr pnodelist; HR = proot-> get_childnodes (& pnodelist); // obtain the number of subnode linked lists. Note that it is equivalent to the number of records obtained. Long productnum = 0; hR = pnodelist-> get_length (& productnum); // calculate the number of child elements. // -------------------------- test the content and name of the child element. Wait msxml2: ixmldomnodeptr pnode = NULL; for (I = 0; I <productnum; I ++) {hR = pnodelist-> get_item (I, & pnode); // obtain the child element cstring STR; pnode-> get_nodename (& name); STR = Name; cout <STR <"; pnode-> get_text (& content); STR = content; cout <STR <"; pnode-> get_nodetype (& type); cout <type <Endl ;}}
Note: The program was copied from the Internet and was modified on its own due to some issues that could not be run at the time.