VC uses MSXML to parse XML documents. Example code

Source: Internet
Author: User

Currently, there are many XML document applications.

I post an example I have previously written for future use.

Part 1: Dom parsing:

Overview: Dom parsing reads a complete XML document and generates a structure tree. This will load all XML documents into the internal. Therefore, the resolution speed will be slower.

1. How to load XML files:

// Create a Dom and load the XML document
MSXML: ixmldomdocumentptr pcommanddoc;
Pcommanddoc. createinstance (_ uuidof (MSXML: domdocument ));
Pcommanddoc-> put_async (variant_false );
Pcommanddoc-> put_validateonparse (variant_false );
Pcommanddoc-> put_resolveexternals (variant_false );
Pcommanddoc-> put_preservewhitespace (variant_true );
Pcommanddoc-> load (file. getbuffer (0 ));

2. Search for the specified node in the XML document:

// Find
MSXML: ixmldomnodeptr prootnode = pcommanddoc-> selectsinglenode ("root/record ");
If (prootnode = NULL)
{
Return;
}

3. Get the node attributes in the XML document.

Cstring strtemp;
MSXML: ixmldomnamednodemapptr pattrs = NULL;
Prootnode-> get_attributes (& pattrs );
If (pattrs = NULL)
{
Return;
}
MSXML: ixmldomnodeptr prequesttypeattr = pattrs-> getqualifieditem ("name ","");
_ Bstr_t strrequesttype = prequesttypeattr-> gettext ();
Strtemp = strrequesttype. Operator char *();

4. Get the node content

_ Bstr_t strvisiport = pnode-> gettext ();

5. Set node content

Hresult hR = pnode-> put_text (_ bstr_t (m_strgatewaypassword ));

6. Set an attribute content
Ixmldomattribute * pA = NULL;
BSTR = sysallocstring (L "attribute 1 ");
Pxmldom-> createattribute (BSTR, & pnode );
Var = variantstring (L "strin ");
Pa-> put_value (VAR );
Proot-> setattributenode (Pa, & pa1 );

Part 2: How to use SAX Parsing

Overview: sax uses a load-type method that separates XML documents and loads them into memory. Event Notification is used to locate the node. It seems that there is no ability to write documents. It is much faster than Dom.

When using sax, You Need To overload an interface isaxcontenthandler in msxml4.0.

After several functions are reloaded, when the node is found, these functions will be called back.

I don't know how to apply this sax parsing mode. It just seems that this method may not work well for that

Parse XML documents with important structures.

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.