Vc msxml getting started with XML operations

Source: Internet
Author: User
Tags xml parser

Recently I have some interest in XML. I want to take a look at it a little. Baidu wrote related articles and tried to write a program to record it for asset preparation.

My development environment: win7 32-bit OS and vs2012.


OK.

PS: As for related theories and so on, let the great fairy heroes worry about it. I will stick to the principles and use them.


Start Visual Studio 2012.

[File] → [new] → [project]

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/58/47/wKiom1StKxLBSKHfAAIyhRVrVOE622.jpg "Title =" creating new project .jpg "alt =" wkiom1stkxlbskhfaaiyhrvrvoe622.jpg "/>

Create an "mfcxmltest" MFC project. [OK]

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/58/47/wKiom1StK8jis03TAAIEopXwIlo695.jpg "Title =" 2_ _1.jpg "alt =" wkiom1stk8jis03taaieopxwilo695.jpg "/>

[Next]

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/58/44/wKioL1StLaPhp8hZAAJa7Yikw2g809.jpg "Title =" 2_ __2.jpg "alt =" wkiol1stlaphp8hzaaja7yikw2g809.jpg "/>

Select "based on the dialog box" to cancel "use Unicode library ". Can be directly [completed]

PS: Unicode does not support Chinese very well.


After the warm-up is completed, start to enter the topic.

First draw a simple program interface.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/58/50/wKiom1SuKkejTycuAABpDYpiquY068.jpg "Title =" 3_ .jpg "alt =" wkiom1sukkejtycuaabpdypiquy068.jpg "/>

Added Chinese characters to test whether Chinese characters are supported.

Control attributes:

Control ID Variable type
Variable name
Idc_edit_id Cstring M_strid
Idc_edit_book Cstring M_strbook
Idc_edit_author Cstring M_strauthor
Idc_button_generate Generate xml
Idc_button_load Load XML


Enter the code writing stage.

Introduce the dynamic link library msxml6.dll (C:/Windows/system32/msxml6.dll) in stdafx. h)

You can add the following at the end of the file:

// Msxml6 is provided in win7. For different operating systems, follow the corresponding version or install # import <msxml6.dll>.


Double-click the "generate xml" button to complete the following code:

// Button: Generate xmlvoid cmfcxmltestdlg: onbnclickedbuttongenerate () {// todo: add the control notification handler code updatedata (true) here; msxml2: ixmldomdocumentptr pdoc; // XML document msxml2: ixmldomelementptr xmlroot; // root node msxml2: ixmldomelementptr pelement, pelement1; // element msxml2: ixmldomnodeptr pnode = NULL; // node msxml2 :: ixmldomprocessinginstructionptr pproinstruction = NULL; // XML declaration // create a domdocument object hresult hR = pdoc. createinstance (_ uuidof (Msxml2: domdocument60); // If (! Succeeded (HR) {MessageBox ("The domdocument object cannot be created. Check whether the ms xml Parser Runtime library is installed! "); Return;} // create an XML document Declaration: <? XML version = "1.0" encoding = "UTF-8"?> Pproinstruction = pdoc-> createprocessinginstruction (_ bstr_t) (char *) "XML", (_ bstr_t) (char *) "version = \" 1.0 \ "encoding = \" UTF-8 \ ""); pdoc-> appendchild (pproinstruction); // The root node name is: library xmlroot = pdoc-> createelement (_ bstr_t) ""); pdoc-> putrefdocumentelement (xmlroot);/* -------- running result -------- <? XML version = "1.0" encoding = "UTF-8"?> <Library> <book id = "321"> <book name> bookname </book name> <author> bookauthor </author> </book> </library> * // Node the name of is bookpelement = pdoc-> createelement (_ bstr_t) "book"); // set the property pelement-> setattribute ("ID", (const char *) m_strid); xmlroot-> appendchild (pelement ); // Add the "title" element pelement1 = pdoc-> createelement (_ bstr_t) ""); pelement1-> puttext (_ bstr_t) (const char *) m_strbook ); pelement-> appendchild (pelement1); // Add the "author" element Pel Ement1 = pdoc-> createelement ("author"); pelement1-> puttext (const char *) m_strauthor); pelement-> appendchild (pelement1 ); /* -------- running result -------- <library> <book> <ID> 123 </ID> <title> bookname </title> <author> bookauthor </author> </book> </library> // Add the "book" Node pnode = pdoc-> createnode (_ variant_t) (long) node_element, (_ bstr_t) (char *) "book", (_ bstr_t) (char *) ""); xmlroot-> appendchild (pnode ); // Add the "ID" element pelement = pdoc-> crea Teelement (_ bstr_t) "ID"); pelement-> puttext (_ bstr_t) (const char *) m_strid); pnode-> appendchild (pelement ); // Add the "title" element pelement = pdoc-> createelement (_ bstr_t) ""); pelement-> puttext (_ bstr_t) (const char *) m_strbook ); pnode-> appendchild (pelement); // Add the "author" element pelement = pdoc-> createelement ("author"); pelement-> puttext (const char *) m_strauthor ); pnode-> appendchild (pelement); * // save it File. If it does not exist, it is created. If it exists, it overwrites pdoc-> Save ("D: // book. xml ");}


Double-click the "load XML" button to complete the following code:

// Button: Load xmlvoid cmfcxmltestdlg: onbnclickedbuttonload () {// todo: add the control notification handler code here/* -------- XML content -------- <? XML version = "1.0" encoding = "UTF-8"?> <Library> <book id = "321"> <book name> bookname </book name> <author> bookauthor </author> </book> </library> * // clear control content m_strid.empty (); m_strbook.empty (); m_strauthor.empty (); updatedata (false); // variable value-> control display msxml2: ixmldomdocumentptr pdoc; hresult hr; HR = pdoc. createinstance (_ uuidof (msxml2: domdocument60); If (failed (HR) {MessageBox ("The domdocument object cannot be created. Check whether the ms xml Parser Runtime library is installed! "); Return;} // load the file pdoc-> load (" D: // book. XML "); // select the content's root node msxml2: ixmldomelementptr pelem = NULL; pelem = pdoc-> selectsinglenode (" // library "); If (pelem = NULL) {return;} // search for the number of "books" in "library" (only one in this example) int nbooknum = pelem-> childnodes-> length; If (nbooknum = 0) {return;} // traverse all bookfor (INT I = 0; I <nbooknum; I ++) {msxml2 :: ixmldomnodeptr pbooknode = pelem-> childnodes-> item [I]; If (pbooknode = NULL) {return ;}// read the "ID" attribute msxml2 of the book node :: ixmldomnodeptr pid = pbooknode-> attributes-> getnameditem ("ID"); m_strid = (lpctstr) PID-> text; // type conversion _ bstr_t → cstring // read the "Name of the book" subnode msxml2: ixmldomnodeptr pbook = pbooknode-> selectsinglenode ("name") under the book node "); if (pbook = NULL) {// cout <"Author: Error \ t";} else {m_strbook = (lpctstr) pbook-> text ;} // read the "author" subnode msxml2: ixmldomnodeptr pauthor = pbooknode-> selectsinglenode ("author"); If (pauthor = NULL) under the book Node) {// cout <"Author: Error \ t";} else {m_strauthor = (lpctstr) pauthor-> text ;}// for (INT I = 0; I <nbooknum; I ++) updatedata (false );}


It's an exciting final moment!

Press F7 to generate the program. If everything goes well, press F5 to start.

Running Effect

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/58/4F/wKioL1SuO8XByHE9AADd8VQ4J_s488.jpg "Title =" 4_runtime result _1.jpg "alt =" wkiol1suo8xbyhe9aadd8vq4j_s488.jpg "/>

\ (Too many rows )/~ La la 650) This. width = 650; "src =" http://img.baidu.com/hi/tsj/t_0003.gif "alt =" t_0003.gif "/>


I encountered an exception during debugging:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/58/4F/wKioL1SuPLrgfum1AACAMC3LYZU086.jpg "Title =" 5_ _1.jpg "alt =" wkiol1suplrgfum1aacamc3lyzu086.jpg "/>

The version of MSXML is not changed to msxml6 on your computer. I hope you will pay attention to it.


PS: Thanks for the help of many netizens!

Http://blog.csdn.net/max2008/article/details/1647613

Http://www.newxing.com/Tech/Program/Cpp/703.html

Http://www.cnblogs.com/xiaoyusmile/p/3920075.html



This article is from the "xingziying" blog, please be sure to keep this source http://xingziying.blog.51cto.com/9815611/1602161

Vc msxml getting started with XML operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.