Write XML files and delete nodes using MFC

Source: Internet
Author: User
Tags processing instruction

From: http://bbs.csdn.net/topics/300026982

The included library files remain unchanged: # import "msxml3.dll"

Namespace unchanged: Using namespace msxml2;

The sample code is as follows:

// Initialization

Coinitialize (null );
// Create a document
Msxml2: ixmldomdocument2ptr pxmldoc = NULL;
Pxmldoc. createinstance ("msxml2.domdocument. 3.0 ");
// Create the Processing Instruction
Msxml2: ixmldomprocessinginstructionptr pxmlproc = NULL;
Pxmlproc = pxmldoc-> createprocessinginstruction ("XML", "version = '1. 0' encoding = 'utf-8 '");
// Insert the Processing Instruction
_ Variant_t vnullval;
Vnullval. Vt = vt_null;
Pxmldoc-> insertbefore (pxmlproc, vnullval );
// Create a root node
_ Variant_t varnodetype (short) msxml2: node_element );
Msxml2: ixmldomnodeptr pxmlnoderoot = NULL;
Pxmlnoderoot = pxmldoc-> createnode (varnodetype, _ T ("root"), _ T (""));
// Add the root node
Pxmldoc-> appendchild (pxmlnoderoot );
// Create and add a subnode
Msxml2: ixmldomnodeptr pxmlnodenode = NULL;
Pxmlnodenode = pxmlnoderoot-> appendchild (pxmldoc-> createelement (_ T ("node1 ")));
Pxmlnodenode = pxmlnoderoot-> appendchild (pxmldoc-> createelement (_ T ("node2 ")));
// Create and set the attributes of the subnode (name, gender, and age)
Msxml2: ixmldomattributeptr pxmlattr = NULL;
Pxmlattr = pxmldoc-> createattribute (_ T ("name "));
Pxmlattr-> nodetypedvalue = "Wang Jin ";
Pxmlnodenode-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmldoc-> createattribute (_ T ("sex "));
Pxmlattr-> nodetypedvalue = "male ";
Pxmlnodenode-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmldoc-> createattribute (_ T ("Age "));
Pxmlattr-> nodetypedvalue = "80 ";
Pxmlnodenode-> attributes-> setnameditem (pxmlattr );
// Create a lower-level Element Node
Msxml2: ixmldomelementptr pxmlele = NULL;
Pxmlele = pxmldoc-> createelement (_ T ("file "));
Pxmlele-> appendchild (pxmldoc-> createtextnode ("element "));
// Add element nodes
Pxmlnodenode-> appendchild (pxmlele );
/*
// Create a lower-level Element Node
Msxml2: ixmldomelementptr pxmlelesub = NULL;
Pxmlelesub = pxmldoc-> createelement (_ T ("file "));
Pxmlelesub-> appendchild (pxmldoc-> createtextnode ("subelement "));
// Add a lower-level Element Node
Pxmlele-> appendchild (pxmlelesub );
*/
// Save the document
Pxmldoc-> Save (_ T ("D: \ test. xml "));

Couninitialize ();

 

Conclusion: when no element exists under a node, the node is not expanded.

 

Delete a node:

Pnodelist-> get_item (I, & pnode );
Pnod-> removechild (pnode, & moldnode );
Moldnode-> release ();

When I used it in the initoxml project, I found that the removechild function only receives one parameter: pnode

In addition, when all nodes are deleted from an index, I in get_item (I, & pnode) cannot increase progressively because each time a node is deleted, the subsequent nodes will be moved to the front.






Related Article

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.