VC parses XML-uses the cmarkup class to parse XML

Source: Internet
Author: User
After trying to parse the XML string using MFC today, we have gained a lot. Let's sum up.
I am using the cmarkup class to operate on XML.

Cmarkup seems to have read the content from an XML file before parsing it. I wish to write my cstring into the XML file and then retrieve it.
If you want to directly read cstring, you can use

Cmarkup: setdoc method.
Other methods are used to repost the examples used by others, which is well written.
FromHttp://hi.baidu.com/yxifu/blog/item/fa1569225bda52a44623e8f0.html

(1) Let's talk about the special characters in XML first, and pay attention when entering them manually.

character entity
& amp; or &
'& apos; or'
& gt; or>
<& lt; or &
"& quot; or"

(2) cmarkup classSource code.

This is the latest version;

This is an example file on the official website. Take out the markup. cpp and markup. H files and import them to your project. The cmarkup class can be used;

:Http://www.firstobject.com/Markup83.zip

(3) create an XML document.

Cmarkup XML;
XML. addelem ("order ");
XML. addchildelem ("item ");
XML. cancelem ();
XML. addchildelem ("Sn", "132487a-j ");
XML. addchildelem ("name", "Crank casing ");
XML. addchildelem ("QTY", "1 ");
XML. Save ("C :\\Userinfo. Xml ");

The effect is as follows:

 <Order>   <Item>    <Sn>  132487a-j  </Sn>    <Name>  Crank Casing  </Name>    <Qty>  1 </Qty>    </Item>    </Order> 
 
(4) browsing specific elements
Cmarkup XML;XML. Load ("userinfo. xml ");
While(XML. findchildelem ("item") {XML. cancelem (); XML. findchildelem ("Sn"); cstring cssn = xml. getchilddata (); XML. findchildelem ("QTY ");IntNqty = atoi (XML. getchilddata (); XML. outofelem ();}
 
(5) add elements and attributes
 
Add at the end, use addelem; Add at the beginning, use insertelem.
 
Cmarkup XML;
 XML. load ("C :\\  userinfo . XML "); XML. addelem ("order"); XML. cancelem ();  // Enter order 

XML. addelem ("item"); XML. cancelem (); // enter item XML. addelem ("Sn", " 4238764-a" ); // Add element XML. addelem ("name", " bearing" ); // Add element XML. addelem ("QTY", " 15" ); // Add element XML. outofelem (); // exit item
XML. addelem ("shipment"); XML. cancelem ();// Enter shipmentXML. addelem ("POC"); // Add the XML. setattrib ("type ","Non-emergency"); // Add the property XML. cancelem ();// Enter the POCXML. addelem ("name ","John Smith"); // Add element XML. addelem (" tel "," 555-1234 "); // Add element XML. Save (" C :\\Userinfo. Xml ");

The effect is as follows:

 <Order>    <Item>   <Sn>  132487a-j  </Sn>    <Name>  Crank Casing  </Name>    <Qty>  1  </Qty>    </Item>   <Item>    <Sn>  4238764-a  </Sn>    <Name>  Bearing  </Name>    <Qty>  15 </Qty>    </Item>    <Shipment>    <POC  Type  ="  Non-emergency  ">    <Name> John Smith  </Name>    <Tel>  555-1234  </Tel>    </Poc>    </Shipment>    </Order>   

(6) modifying elements and attributes

For example, change the attribute type in POC to: change;

Change element Tel to: 123456789

Cmarkup XML;
If (XML. Load ("userinfo. xml "))
{
Cstring struserid = _ T ("");
XML. resetmainpos ();
If (XML. findchildelem ("shipment "))
{
XML. cancelem ();
If (XML. findchildelem ("POC "))
{
XML. cancelem ();
Cstring str_type = xml. getattrib ("type ");
MessageBox (str_type );
XML. setattrib ("type", "change ");
Struserid = xml. getdata ();

If (XML. findchildelem ("tel "))
{
XML. cancelem ();
XML. setdata ("123456789 ");
XML. Save ("userinfo. xml ");
Return;
}
}
}
}

(7) delete elements:

Delete the project with Sn = 132487a-j.

Cmarkup XML;
If (XML. Load ("userinfo. xml "))
{
Cstring struserid = _ T ("");
XML. resetmainpos ();
If (XML. findchildelem ("item "))
{
XML. cancelem ();
Cstring str_sn;
XML. findchildelem ("Sn ");
Str_sn = xml. getchilddata ();
If (str_sn = "132487a-j ")
{
XML. removeelem ();
XML. Save ("userinfo. xml ");
}
}
}

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.