第二个子节点的XML路径
XML Path to second child
我想通过path(不迭代)为ptree中的子节点添加一个新值
为例:
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
int main() {
using boost::property_tree::ptree;
ptree main;
ptree children;
children.add("Foo", "bar");
main.add_child("child", children);
main.add_child("child", children);
ptree newChildren("Foo");
main.put("child{2}.Foo", newChildren.data()); // <-- Access second element?
// Output
boost::property_tree::xml_writer_settings<std::string> settings('t', 1);
boost::property_tree::write_xml(std::cout, main, settings);
std::cin.ignore();
return 0;
}
问题是,我不能通过路径访问第二个子节点。有什么格式是有效的吗?
我指的是{2}部分:
"child{2}.Foo"
I tried <2>,[2],(2)…没有运气…(
我还有希望吗?谢谢你!
我再说一遍:Boost中没有xml解析器/库。
你(ab)使用的是Boost属性树。正如您所发现的,它是一个"属性树"库,这意味着它可以做一些事情。包括写和读属性树。
如果您想要通用的XML内容,请考虑使用XML库(在c++中应该使用哪种XML解析器?)。
废话少说:
Live On Coliru
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
int main() {
using boost::property_tree::ptree;
ptree pt;
{
ptree children; children.add("Foo", "bar");
pt.add_child("child", children);
pt.add_child("child", children).put("MARK","so we know it's the second");
}
// Output
boost::property_tree::xml_writer_settings<std::string> settings('t', 1);
//boost::property_tree::write_xml(std::cout, pt, settings);
auto child1 = std::find_if(pt.begin(), pt.end(), [](auto& node) { return node.first == "child"; });
auto child2 = std::find_if(std::next(child1), pt.end(), [](auto& node) { return node.first == "child"; });
if (child2 != pt.end())
{
boost::property_tree::write_xml(std::cout, child2->second, settings);
ptree newChildren("Foo");
child2->second.put("Sub.Foo", newChildren.data()).put("BYE", "ALL DONE"); // <-- Access second element?
boost::property_tree::write_xml(std::cout << "nnAFTER EDITING:n", child2->second, settings);
}
}
打印:
<?xml version="1.0" encoding="utf-8"?>
<Foo>bar</Foo>
<MARK>so we know it's the second</MARK>
AFTER EDITING:
<?xml version="1.0" encoding="utf-8"?>
<Foo>bar</Foo>
<MARK>so we know it's the second</MARK>
<Sub>
<Foo>
Foo
<BYE>ALL DONE</BYE>
</Foo>
</Sub>
使用helper函数改进样式:
Live On Coliru
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
template <typename Tree, typename Out>
Out find_all(Tree& pt, typename Tree::path_type path, Out out) {
if (path.empty()) {
*out++ = pt;
return out;
}
auto head = path.reduce();
for (auto& child : pt)
if (child.first == head)
out = find_all(child.second, path, out);
return out;
}
int main() {
using boost::property_tree::ptree;
ptree pt;
{
ptree children; children.add("Foo", "bar");
pt.add_child("child", children);
pt.add_child("child", children).put("MARK","so we know it's the second");
}
// Output
boost::property_tree::xml_writer_settings<std::string> settings('t', 1);
//boost::property_tree::write_xml(std::cout, pt, settings);
std::vector<std::reference_wrapper<ptree> > matches;
find_all(pt, "child", back_inserter(matches));
ptree& child2 = matches.at(1);
child2.put("BYE", "ALL DONE");
boost::property_tree::write_xml(std::cout, child2, settings);
}
打印
<?xml version="1.0" encoding="utf-8"?>
<Foo>bar</Foo>
<MARK>so we know it's the second</MARK>
<BYE>ALL DONE</BYE>
相关文章:
- Cppcheck生成xml转储文件
- 如何将更多文件夹添加到c++include路径
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- C++A*算法并不总是在路径中具有目标节点
- 从函数角度看ID到文件路径的内部与外部映射
- 如何在pugixml中获取节点的内部XML
- 如何使用tinyxml2从XML加载父实体和子实体
- boost xml parsingl将xml的路径作为变量发送
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- 如何使用cppcheck处理半相对包含路径
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 在C++中设置基于操作系统的文件路径
- 如何让用户在 tinyxml2 中输入 XML 文件的路径
- 从路径中读取 xml
- Xmllint未设置,而在路径中找不到XMLLINT;跳过XML预处理
- 带有相对路径的Pugi XML
- IOS上的OpenCV haar级联了C++中的xml文件路径问题
- 第二个子节点的XML路径
- Xml文件保存在两个不同的路径
- Visual c++图像文件路径转换成xml