将字符串转换为 XML 并使用提升更新值字段
convert string to xml and update value field using boost
>我有这样的字符串
> <?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);
相关文章:
- 将结构字段的类型展开为可变模板参数
- 将位字段导出到数组
- 为了方便起见,我应该避免公开私有字段变量吗
- 当字段可以为null时,如何使用C++接口在Avro中写入数据
- 在java中读取c++字节的位字段
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- 私有字段对象与私有继承?
- 声明没有默认构造函数的字段
- C++内存模型和位字段的最大序列
- 声明为无效的变量或字段'...' Ardunio 编译器上的错误
- 如何在QByteArray中放置和检索位字段而不会感到痛苦?
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- 如何通知侦听器结构的哪个字段已更新
- 将字符串转换为 XML 并使用提升更新值字段
- 将图像字段作为斑点类型更新为QT C 中的数据库
- 在AD中尝试更新我自己的字段时,一般访问被拒绝
- 如何在不输出新行的情况下更新终端中的输出字段
- 无法更新相对文件中的记录字段,C++
- 日志含义更新类字段后出现分段错误
- DELETE查询后更新自动编号字段