boost 属性 tree 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 调用,它将插入路径指定为“./”
什么都不指定会导致异常,这是我发现的唯一可行的方法。
已阅读 the header file for 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
成员。
这是一个将 OLD VALUE
替换为 NEW VALUE
的示例:
#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);
}
我正在使用 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 调用,它将插入路径指定为“./”
什么都不指定会导致异常,这是我发现的唯一可行的方法。
已阅读 the header file for 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
成员。
这是一个将 OLD VALUE
替换为 NEW VALUE
的示例:
#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);
}