• rapidxml修改节点的值


    1、rapidxml修改节点的value,修改之后,序列化还是原来的值,具体原因是什么,要看rapidxml是怎么实现的。如下:

    void TestRapidXml()
    {
        char* xmlContent = new char[1024];
        sprintf(xmlContent,"<root><head>aaa</head><body x="10">bbb</body></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("20");    
    
        string xmlStr = "";
        // xmlString为 <root><head>aaa</head><body x="20">bbb</body></root>
        // 也就是说,attr的value可以修改成功,而node的value还是旧值。
        rapidxml::print(std::back_inserter(xmlStr),xmlDoc,0); 
        
        delete []xmlContent;
    }

    2、怎么解决上面的问题,笨办法,既然不能修改,我就添加一个新的,删除老的。如下:

    void TestRapidXml()
    {
        char* xmlContent = new char[1024];
        sprintf(xmlContent,"<root><head>aaa</head><body x="10">bbb</body></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"));
        // 插入一个新的body
        root->insert_node(body,newBody);
        
        // 复制老body的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);
        }
        // 删除老的body
        root->remove_node(body);
    
        string xmlStr = "";
        // xmlString为 <root><head>aaa</head><body x="10">ccc</body></root>
        rapidxml::print(std::back_inserter(xmlStr),xmlDoc,0); 
        
        delete []xmlContent;
    }

    3、还有一个办法,就是使用 xmlDoc.parse<parse_no_data_nodes>(xmlContent); 如下:

    void TestRapidXml()
    {
        char* xmlContent = new char[1024];
        sprintf(xmlContent,"<root><head>aaa</head><body x="10">bbb</body></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("20");    
    
        string xmlStr = "";
        // xmlString为 <root><head>aaa</head><body x="20">ccc</body></root>
        rapidxml::print(std::back_inserter(xmlStr),xmlDoc,0); 
        
        delete []xmlContent;
    }
  • 相关阅读:
    Monkey稳定性测试实战之Monkey对APP随机测试
    自动化测试用例选型及手工对比自动化测试优缺点
    自动化测试如何解决验证码的问题
    Selenium家谱
    Python基础入门-数据类型
    with open
    Python开源机器学习框架:Scikit-learn六大功能,安装和运行Scikit-learn
    Python的可视化包 – Matplotlib 2D图表(点图和线图,.柱状或饼状类型的图),3D图表(曲面图,散点图和柱状图)
    python -- numpy 基本数据类型,算术运算,组合,分割 函数
    python pandas ---Series,DataFrame 创建方法,操作运算操作(赋值,sort,get,del,pop,insert,+,-,*,/)
  • 原文地址:https://www.cnblogs.com/nzbbody/p/4509266.html
Copyright © 2020-2023  润新知