Learning examples for C + + operations with MSXML

Source: Internet
Author: User

Recent work needs, need to use C + + to interact with XML, in the course of learning encountered some problems, from the Internet also searched for some information, summarized here, is not only a beginner's carding, but also to facilitate the need to contact people.

    1. The understanding of XML.

XML is an explanatory language of relative canonical thorough. The specific syntax you can check, for our use of the angle, we just need to understand that he has a lot of nodes (elements), from the parent element to its child elements, child elements can be empty, the resource has properties, you can easily store data. The following is an example document of XML:

<?xml Version = "1.0" encoding= "UTF-8"? ><picture><circle><circle name= "Circle1" radius= "12.5"/ ><circle name= "Circle2" radius= "56.5"/></circle><rectangle><rectangle name = "Rectangle1" Rec_long= "20.5" rec_weight= "/><rectangle name =" Rectangle1 "rec_long=" "rec_weight="/>< " Rectangle name = "Rectangle1" rec_long= "rec_weight="/></rectangle></picture>

2. Preparation for use of this instance

The platform we use is VS2005, using MSXML4, ready for use before working as follows:

    1. Download and install MSXML4;

    2. Using VS2005 to create a new project, I'm using a dialog box project here. This uses a tree control (Treecontrol)

    3. Add a statement at the end of the stdafx.h header file for the project:

      #import "Msxml4.dll"
      using namespace MSXML2;

      I encountered a problem here: When using the interface always quote "ERROR:XX function: Not sure of the symbol", which makes me very confused, even if my newly inserted msxml4.dll may conflict, how can I report an error? And I also used the using namespace The MSXML2 statement. The solution to the problem is to add MSXML2: scope when the next interface is used. The reason for the problem is that there is a conflict with the use of the using namespace that we vs2005 create the project with the header file plus msxml2.h.

3. Instructions and understanding of the operating interface. (This text is from the network)

can refer to a blog post, http://blog.csdn.net/perddy/article/details/1756481, there is an explanation of the interface throughout, here we can point to a point: in the DOM interface specification, There are four basic interfaces: Document,node,nodelist and NamedNodeMap. In these four basic interfaces, the document interface is the entry that operates on documents and is inherited from the node interface. The node interface is the parent of most other interfaces , such as Documet,element,attribute, Text,comment, and so on, which are inherited from the node interface. The NodeList interface is a collection of nodes that contain all the child nodes in a node. The NamedNodeMap interface is also a collection of nodes that can be used to establish a one by one mapping relationship between node names and nodes, thus allowing direct access to specific nodes using node names.

4. For the purpose of our code, manipulate the XML document above to obtain some related properties.

CoInitialize (NULL);//Initialize COM resource   //ixmldocumentptr xmlfile=null; ccomptr<msxml2::i xmldomdocument> spxmldoc; //Initialize XMLFile object    hresult hr= Spxmldoc.cocreateinstance (__uuidof (MSXML2::D OMDocument40))  ccomvariant varxml (_t ("d:\\testadoproj\\ Picturedata.xml "));  //spxmldoc->put_async (VARIANT_FALSE);//Set to Synchronous  if (FAILED (HR))  {   cstring errormess= "failed to create xmlinsatance! ";   afxmessagebox (errormess); } else  {  try  {    spxmldoc->load (varxml);//Load Xml  }  catch (_com_error e)   {    afxmessagebox (E.errormessage ());    cstring errorsee= "Connection XML error. ";    afxmessagebox (Errorsee);  } }  msxml2::ixmldomelementptr  childnode; cstring senoname=_t ("//circle");  childnode= (msxml2::ixmldomelementptr) (spXmldoc-> selectSingleNode ((_bstr_t) (Senoname));  long ncount,icount; // msxml2::ixmldomnodelist * xmlchildnodes= Null; childnode->get_childnodes (&xmlchildnodes);//gets child elements and joins to the list  xmlchildnodes->get_length ( &icount);  //gets the number of its child nodes, Circlex below has two empty elements   only the property values are different  //targetnode = selectsinglenode (" nodename[@NodeAtt = ' nodeattvalue '] ")  //if the child element is not empty  if  (icount>0)  {  msxml2::i xmldomnode *currentnode =null;  msxml2::ixmldomnodeptr preturnnode =null;   for (int i=0;i<icount;i++)   {   xmlchildnodes->get_item (i,& CurrentNode);    preturnnode=currentnode->getattributes ()->getnameditem (_T ("radius"));    _bstr_t temp_radius;   temp_radius=preturnnode->gettext ();    addtoitem (Temp_radius);   }   }//remember to have the relevant release pointers, add CoUninitialize () yourself;//release COM resources

5. Note the use of smart pointers with respect to MSXML.

Ask another question: IXMLDOMNode * What is the difference between defining pointers and IXMLDOMNODEPTR definitions?

Write these for the time being, and later in the detailed.

This article is from the "technology does not stop" blog, please be sure to keep this source http://3994129.blog.51cto.com/3984129/1575151

Learning examples for C + + operations with MSXML

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.