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