我有一根这样的绳子
> <?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;
}发布于 2019-04-09 08:11:15
您希望修改某个节点中的数据,因此调用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的前提下迭代元素,因为您修改了项:
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);https://stackoverflow.com/questions/55587743
复制相似问题