Access XML files with VC ++

Source: Internet
Author: User

Using Microsoft's Dom,
Msxml4
// Introduce msxml4.dll
# Import "C:/winnt.0/system32/msxml4.dll"

// Create the xmldomdocument pointer
Msxml2: ixmldomdocumentptr pxmldoc;

// Initialize the COM interface
: Coinitialize (null );
// Use a smart pointer to create an instance of the parser
Hresult hr;
HR = pxmldoc. createinstance (_ uuidof (msxml2: domdocument30 ));
// Load the file
Pxmldoc-> load ("F: // He. xml ");
// Search for a node named city in the tree. "//" indicates searching at any Layer
Msxml2: ixmldomelementptr childnode;
Childnode = (msxml2: ixmldomelementptr) (pxmldoc-> selectsinglenode ("// Author "));
// Obtain the node type
Msxml2: domnodetype nodetype;
Childnode-> get_nodetype (& nodetype );
// Node name
BSTR var;
Cstring name;
Childnode-> get_nodename (& var );
Name = (char *) (_ bstr_t) var;
// Node Value
Variant varval;
Childnode-> get_nodetypedvalue (& varval );
Cstring strvalue = (char *) (_ bstr_t) varval;
Read attributes:
// Store node attributes in the linked list
Msxml2: ixmldomnamednodemapptr pattrs = NULL;
Msxml2: ixmldomnodeptr pattritem;
Childnode = (msxml2: ixmldomelementptr) (pxmldoc-> selectsinglenode ("// num "));

Childnode-> get_attributes (& pattrs );
Long ncount;
Pattrs-> get_length (& ncount );
For (INT I = 0; I <ncount; I ++)
{
Pattrs-> get_item (I, & pattritem );
// You can use the get_nodename and get_nodetypedvalue functions to obtain the attribute name and attribute value.
// You can also directly obtain
Cstring strattrname = (char *) (_ bstr_t) pattritem-> nodename;
Cstring strattrvalue = (char *) (_ bstr_t) pattritem-> nodetypedvalue;
}

In addition
You can use MSXML dom

The process of creating a document object.
Hresult hr;
Ixmldomdocument * pxmldoc;
Ixmldomnode * pxdn;
HR = coinitialize (null); // initialization of COM

HR = cocreateinstance (clsid_dom document, null, clsctx_inpproc_server,
Iid_ixmldomdocument, (void **) & pxmldoc );
HR = pxmldoc-> QueryInterface (iid_ixmldomnode, (void **) & pxdn );
Use the createelement method in the document to create a node to load and save XML files. The load or loadxml method can be used to load an XML document from a specified URL. The save method is used to save the document to a specified location, and getelementsbytagname is used to obtain the node data.

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.