Boost属性树add_child指定路径
boost property tree add_child specifying the path
我使用boost::property_tree来操作一些XML。我必须在xml文档中添加子节点
xml文档看起来像这样:
<MPD>
<Period>
<AdaptationSet mimeType="audio/mp4" segmentAlignment="true" startWithSAP="1">
<ContentProtection schemeIdUri="urn:mpeg:dash:mp4protection:2011" value="cenc"/>
<ContentProtection schemeIdUri="urn:uuid:9a04f079-9840-4286-ab92-e65be0885f95">
<!-- value already exists here //-->
<!-- need to insert new entry here //-->
</ContentProtection>
</AdaptationSet>
<AdaptationSet>
<ContentProtection ... />
<ContentProtection ... />
<ContentProtection ... />
</AdaptationSet>
</Period>
</MPD>
因此,我使用以下代码将新条目插入到上面XML中标记的位置:
typedef boost::property_tree::iptree property_tree_t;
typedef boost::shared_ptr<property_tree_t> shared_ptree_t;
typedef boost::optional<property_tree_t &> optional_ptree_t;
string sSearchSchemeIdUri = "urn:uuid:9a04f079-9840-4286-ab92-e65be0885f95";
string sChildName;
optional_ptree_t spPeriod = spPTree->get_child_optional("MPD.Period");
if (spPeriod)
{
for (auto & adaptSetNextChild : spPeriod->get_child("AdaptationSet"))
{
sChildName = adaptSetNextChild.first;
if (sChildName.compare("ContentProtection") == 0)
{
property_tree_t & ptContentProtection = adaptSetNextChild.second;
string sSchemeIdUri = ptContentProtection.get<string>("<xmlattr>.schemeIdUri", "");
if (sSchemeIdUri.compare(sSearchSchemeIdUri) == 0)
{
property_tree_t ptPssh;
ptPssh.clear();
ptPssh.add("cenc:PSSH", sPssh);
ptContentProtection.add_child("./", ptPssh);
}
}
}
}
上面的代码将添加的数据包装在下面:
<></>
<//></>
我想避免。
这个错误的标记来自我的add_child调用,它将插入路径指定为"./"
不指定会导致异常,这是我发现的唯一有效的方法。
读取了ptree.hpp的头文件,其中声明参数为
/** Add the node at the given path. Create any missing parents. If there
* already is a node at the path, add another one with the same key.
* @param path Path to the child. The last fragment must not have an
* index.
* @return A reference to the inserted subtree.
* @note Because of the way paths work, it is not generally guaranteed
* that a node newly created can be accessed using the same path.
*/
self_type &add_child(const path_type &path, const self_type &value);
是否有任何方法可以指定当前路径,例如:ptContentProtection的路径?
您可能希望使用put
成员而不是add
成员。
下面是用NEW VALUE
代替OLD VALUE
的示例:
Live On Coliru
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
static std::string const sample = R"(<MPD>
<Period>
<AdaptationSet mimeType="audio/mp4" segmentAlignment="true" startWithSAP="1">
<ContentProtection schemeIdUri="urn:mpeg:dash:mp4protection:2011" value="cenc"/>
<ContentProtection schemeIdUri="urn:uuid:9a04f079-9840-4286-ab92-e65be0885f95">OLD VALUE</ContentProtection>
</AdaptationSet>
<AdaptationSet>
</AdaptationSet>
</Period>
</MPD>)";
static std::string sSearchSchemeIdUri = "urn:uuid:9a04f079-9840-4286-ab92-e65be0885f95";
int main() {
using boost::property_tree::iptree;
iptree pt;
{
std::istringstream iss(sample);
read_xml(iss, pt);
}
for (auto& as : pt.get_child("MPD.Period")) {
if (as.first == "AdaptationSet") {
for (auto& cp : as.second) {
if (cp.first == "ContentProtection" && cp.second.get("<xmlattr>.schemeIdUri", "") == sSearchSchemeIdUri)
{
cp.second.put_value("NEW VALUE");
}
}
}
}
auto xws = boost::property_tree::xml_writer_make_settings<std::string>(' ', 4);
write_xml(std::cout, pt, xws);
}
相关文章:
- 如何将更多文件夹添加到c++include路径
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- C++A*算法并不总是在路径中具有目标节点
- 从函数角度看ID到文件路径的内部与外部映射
- boost xml parsingl将xml的路径作为变量发送
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- 如何使用cppcheck处理半相对包含路径
- 在C++中设置基于操作系统的文件路径
- 基于编译器选项的编译二进制路径
- 按边长度递归搜索图中所有可行路径
- 使用变量值作为 PlaySound 中的路径
- 更改"child's parent's style sheet"时如何将孩子的样式表重置为 Qt 默认样式表?
- 如何转换真实路径 CString c++
- 从 GUID 获取 USB 卷路径
- 查找 GCD:并非所有控制路径都返回值
- C++17 文件系统::remove_all 带有通配符路径
- 在带有尾部斜杠的路径上返回 std::filesystem::create_directories() 的值
- 获取当前正在运行的 exe 名称(不是路径)
- 如何从 Skia 路径几何体中获取网格?
- 如何使用 boost::p rocess::child 处理路径中的空格