Rapidxml Modifying the value of a node

Source: Internet
Author: User
Tags sprintf

1, Rapidxml Modify the value of the node, after modification, serialization or the original value, the specific reason is what, to see how rapidxml is implemented. As follows:

voidTestrapidxml () {Char* Xmlcontent =New Char[1024x768]; sprintf (Xmlcontent,"<root>"); Xml_document<>xmldoc; Xmldoc.parse<0>(xmlcontent); Xml_node<>* BODY = Xmldoc.first_node ()->first_node ("Body"); Body->value ("CCC"); Xml_attribute<>* x = Body->first_attribute ("x"); X->value (" -"); stringXmlstr =""; //xmlstring for <root>//In other words, the value of attr can be modified successfully, and node's value is the old value. Rapidxml::p rint (Std::back_inserter (XMLSTR), xmldoc,0); delete []xmlcontent;}

2, how to solve the above problem, stupid method, since can not be modified, I will add a new, delete the old. As follows:

voidTestrapidxml () {Char* Xmlcontent =New Char[1024x768]; sprintf (Xmlcontent,"<root>"); Xml_document<>xmldoc; Xmldoc.parse<0>(xmlcontent); Xml_node<>* root =Xmldoc.first_node (); Xml_node<>* BODY = Root->first_node ("Body"); Xml_node<>* Newbody =Xmldoc.allocate_node (node_element, xmldoc.allocate_string ("Body"), Xmldoc.allocate_string ("CCC")); //insert a new bodyRoot->Insert_node (body,newbody); //copy the old body's attr     for(xml_attribute<>* attr = Body->first_attribute ();attr!=null;attr=attr->Next_attribute ()) {Xml_attribute<>* copy = Xmldoc.allocate_attribute (xmldoc.allocate_string (attr->name ()), Xmldoc.allocate_string (xmldoc.allocate_string (attr-value ()))); Newbody-append_attribute (copy); }    //remove the old bodyRoot->Remove_node (body); stringXmlstr =""; //xmlstring for <root>Rapidxml::p rint (Std::back_inserter (XMLSTR), xmldoc,0); Delete[]xmlcontent;}

3, there is another way, is to use xmldoc.parse<parse_no_data_nodes> (xmlcontent); As follows:

voidTestrapidxml () {Char* Xmlcontent =New Char[1024x768]; sprintf (Xmlcontent,"<root>"); Xml_document<>xmldoc; //xmldoc.parse<0> (xmlcontent);Xmldoc.parse<parse_no_data_nodes>(xmlcontent); Xml_node<>* BODY = Xmldoc.first_node ()->first_node ("Body"); Body->value ("CCC"); Xml_attribute<>* x = Body->first_attribute ("x"); X->value (" -"); stringXmlstr =""; //xmlstring for <root>Rapidxml::p rint (Std::back_inserter (XMLSTR), xmldoc,0); Delete[]xmlcontent;}

Rapidxml Modifying the value of a node

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.