Some basic implementation methods for using msxml dom in VC

Source: Internet
Author: User
Known variables
Msxml2: ixmldomdocument * pdoc;
Msxml2: ixmldomnode * pchild, * pparent;
Msxml2: ixmldomnode * pnod;
Msxml2: ixmldomelement * Pele;

Initialization pointer:
Msxml2: ixmldomdocument * pdocument = NULL;
Msxml2: ixmldomnodelist * pnodelist = NULL;
Msxml2: ixmldomnamednodemap * pnodemap = NULL;
Msxml2: ixmldomnode * pnode = NULL
Msxml2: ixmldomtext * ptext = NULL;
Msxml2: ixmldomelement * pelement = NULL;
Msxml2: ixmldomprocessinginstruction * pprocessinginstruction = NULL;
Msxml2: ixmldomcomment * pcomment = NULL;

Msxml2: ixmldomparseerror * pobjerror = NULL;

Known Data:
BSTR bstrtext, bstrname;
Int type;

**) Create a new document
// HR = cocreateinstance (clsid_domdocument, null, clsctx_inproc_server,
// Iid_ixmldomdocument, (void **) & m_pxmldoc );
Hresult hR = cocreateinstance (_ uuidof (msxml2: domdocument40), null, clsctx_inproc_server,
_ Uuidof (msxml2: ixmldomdocument), (void **) & m_pxmldoc );
HR = pdocument-> put_async (variant_false );
HR = pdocument-> put_validateonparse (variant_false );
HR = pdocument-> put_resolveexternals (variant_false );
 

A) add a child node to the parent node
Pparent-> appendchild (pchild, & pnode)
 

B) Create a node
Variant vtype;
Vtype. Vt = vt_i4;
V_i4 (& vtype) = (INT) type;
Pdoc-> createnode (vtype, bstrname, null, & pnode );

C) Create an element node
Pdoc-> createelement (bstrname, pelement );
(Pelement)-> put_text (bstrtext );

D) Create a text subnode and add it to the parent node.
Pdoc-> createtextnode (bstrtext, & ptext );
Pparent-> appendchild (ptext, & pnode)

E) Create // create a Processing Instruction element.
BSTR bstrtarget = sysallocstring (L "XML ");
BSTR bstrdata = sysallocstring (L "version = '1. 0 '");
Pdoc-> createprocessinginstruction (bstrtarget, bstrdata, & pprocessinginstruction );
Sysfreestring (bstrtarget );
Sysfreestring (bstrdata );

F) Create a comment Node
Pdoc-> createcomment (bstrtext, & pcomment );

G) Element Node Attribute Value
Get Attribute Value
Variant V;
Pele-> getattribute (bstrname, & V );
Cstring STR = V. bstrval;
Set attribute values
Ccomvariant V (STR );
Pele-> setattribute (bstrname, V );

H) node attribute value
variant V;
cstring STR;
long mcount;
get the node attribute set
hR = pnod-> get_attributes (& pnodemap);
hR = pnodemap-> get_length (& mcount );
Get node attributes
hR = pnodemap-> getnameditem (bstrname, & pnode);
pnodemap-> get_item (I, & pnode );
get the node property value
hR = pnode-> get_node value (& V);
STR = v. bstrval;
delete node attributes
msxml2: ixmldomnode * moldnode;
mnodemape-> removenameditem (bstrname, & moldnode);
If (moldnode! = NULL) moldnode-> release ();

I) node
(1) obtain the node and Node Set
Based on the node name
Pdoc-> getelementsbytagname (bstrname, & pnodelist );
Pnodelist-> get_item (0, & pnode );
Obtain the subnode set.
HR = pnod-> get_childnodes (& mnodelist );
Long mcount;
Msxml2: ixmldomnode * pnodesub;
Mnodelist-> get_length (& mcount );
HR = pnode-> get_firstchild (& pnodesub );
Delete a subnode (including deleting node attributes, subnodes, and current nodes)
Delete the current subnode
Pnodelist-> get_item (I, & pnode );
Pnod-> removechild (pnode, & moldnode );
Moldnode-> release ();

(2) Get the node name based on the node
Pnod-> get_nodename (& bstrname );
(3) obtain the node value based on the node
Variant V;
HR = pnod-> get_nodeValue(& V );

J) XML file operations
Load
Variant_bool status;
Variant vfilename;
(1)
Pdoc-> loadxml (bstrname, & status );
(2)
V_bstr (& vfilename) = strfilename. allocsysstring ();
V_vt (& vfilename) = vt_bstr;
Vresult = pdoc-> load (vfilename, & status );
Save
BSTR pbfname = mfilename. allocsysstring ();
Ccomvariant V (pbfname );
HR = pdoc-> Save (v );

BSTR = NULL;
Pdoc-> get_xml (& BSTR );

K) handle errors
BSTR = NULL;
Pdoc-> get_parseerror (& pobjerror );
Pobjerror-> get_reason (& BSTR );
Afxmessagebox (_ T ("failed to load DOM from books. xml. % s \ n"), * BSTR );

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.