XML 第二个路径 child
XML Path to second child
我想通过路径(无迭代)将新值放入 child 内的 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;
}
问题是,我无法通过路径访问第二个 child。有什么格式可以用吗?
我是说这 {2} 部分:
"child{2}.Foo"
我尝试了 <2>、[2]、(2)...没有成功...:(
我有希望吗?谢谢!
我再说一遍:Boost 中没有 xml parser/library。
您正在(滥用)使用的是 Boost 属性 树。如您所见,它是一个 "property tree" 库,这意味着它可以做一些事情。包括写作和阅读属性棵树。
如果您想要通用 XML 东西,请考虑使用 XML 库 (What XML parser should I use in C++?)。
事不宜迟:
#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 << "\n\nAFTER 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>
更新
使用辅助函数改进样式:
#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>
我想通过路径(无迭代)将新值放入 child 内的 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;
}
问题是,我无法通过路径访问第二个 child。有什么格式可以用吗?
我是说这 {2} 部分:
"child{2}.Foo"
我尝试了 <2>、[2]、(2)...没有成功...:(
我有希望吗?谢谢!
我再说一遍:Boost 中没有 xml parser/library。
您正在(滥用)使用的是 Boost 属性 树。如您所见,它是一个 "property tree" 库,这意味着它可以做一些事情。包括写作和阅读属性棵树。
如果您想要通用 XML 东西,请考虑使用 XML 库 (What XML parser should I use in C++?)。
事不宜迟:
#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 << "\n\nAFTER 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>
更新
使用辅助函数改进样式:
#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>