将字符串转换为 XML 并使用提升更新值字段

convert string to xml and update value field using boost

本文关键字:更新 字段 转换 字符串 XML      更新时间:2023-10-16

>我有这样的字符串

> <?xml version="1.0" encoding="utf-8"?> <Parameters>
>     <Parameter id="ID_1" value="1"/>
>     <Parameter id="ID_2" value="000293604959"/>
>     <Parameter id="ID_3" value="MIIDzzCCAregAwIBAgIQBU7WUKqJI"/>          //Variable length data
>     <Parameter id="ID_4" value="MIIDyDCCArCgAwIBAgIGAWSjA2NaMA0GCSqGS"/>  //Variable length data
>     <Parameter id="ID_5" value="MIIDcDCCAligAwIBAgIEATMzfzANBgkqhk"/>     //Variable length data
>     <Parameter id="ID-6" value="WIN_10_64_bit"/> </Parameters>

我希望将其打印为(预期输出(意味着隐藏ID_3、ID_4和ID_5数据。

> <?xml version="1.0" encoding="utf-8"?> <Parameters>
>     <Parameter id="ID_1" value="1"/>
>     <Parameter id="ID_2" value="000293604959"/>
>     <Parameter id="ID_3" value="Sensitive Data"/>         //Variable length data
>     <Parameter id="ID_4" value="Sensitive Data"/>         //Variable length data
>     <Parameter id="ID_5" value="Sensitive Data"/>         //Variable length data
>     <Parameter id="ID-6" value="WIN_10_64_bit"/>

到目前为止,我已经尝试过这个,但没有运气。请让我知道我在哪里做错了将值字段更新为敏感数据的实际值。

void hideData(string request)
{
stringstream ss;
ss.str(request);
boost::property_tree::ptree pt1;
boost::property_tree::read_xml(ss, pt1);
ostringstream oss;
string finalStringToPrint;
if (!pt1.empty())
{
    BOOST_FOREACH(boost::property_tree::ptree::value_type const& node, pt1.get_child("Parameters"))
    {
        if (node.first == "Parameter")
        {
            string id = node.second.get_child("<xmlattr>.id").data();
            if (id == "ID_3")
            {
                string value = node.second.get_child("<xmlattr>.value").data();
                value.erase();
                value.assign("Sensitive Data");
                pt1.put_value(value);
            }
            if (id == "ID_4")
            {
                string value = node.second.get_child("<xmlattr>.value").data();
                value.erase();
                value.assign("Sensitive Data");
                pt1.put_value(value);
            }
            if (id == "ID_5")
            {
                string value = node.second.get_child("<xmlattr>.value").data();
                value.erase();
                value.assign("Sensitive Data");
                pt1.put_value(value);
            }
        }
    }
}
boost::property_tree::write_xml(oss, pt1, boost::property_tree::xml_writer_make_settings<string>(' ', 4));
finalStringToPrint = oss.str();
cout << finalStringToPrint << endl;
}

你想修改某个节点中的数据,所以调用 get_child ,保留对返回的子节点的引用,然后在此节点上调用put_value

            if (id == "ID_3")
            {
                auto& child = node.second.get_child("<xmlattr>.value");
                child.put_value("Sensitive Data");
            }
            if (id == "ID_4")
            {
                auto& child = node.second.get_child("<xmlattr>.value");
                child.put_value("Sensitive Data");
            }
            if (id == "ID_5")
            {
                auto& child = node.second.get_child("<xmlattr>.value");
                child.put_value("Sensitive Data");
            }

在迭代时,您需要迭代元素而不const foreach,因为您修改了项目:

BOOST_FOREACH( boost::property_tree::ptree::value_type & node, pt1.get_child("Parameters"))

编辑

要删除空格,您可以使用property_tree::xml_parser::trim_whitespace为输入read_xml

boost::property_tree::read_xml(ss, pt1, boost::property_tree::xml_parser::trim_whitespace);