如何在boost::property_tree中迭代XML结构
How to iterate over XML structure in boost::property_tree
我有一个XML结构,如下所示:
<root>
<SomeElement>
<AnotherElement>
<ElementIWant x="1" y="1"/>
</AnotherElement>
</SomeElement>
<SomeElement>
<AnotherElement>
<ElementIWant x="1" y="1"/>
<ElementIWant x="2" y="1"/>
<ElementIWant x="3" y="1"/>
</AnotherElement>
</SomeElement>
</root>
被读入boost::property_tree
,有1..许多 <SomeElement>
s,然后在该元素的任意深度可能有1..许多 <ElementIWant>
s
是否有一种方法可以按照它们出现在文档中的顺序直接(在单个循环中)迭代<ElementIWant>
?
我看过equal_range
void iterateOverPoints()
{
const char* test =
"<?xml version="1.0" encoding="utf-8"?><root>"
"<SomeElement>"
"<AnotherElement>"
"<ElementIWant x="1" y="1"/>"
"</AnotherElement>"
"</SomeElement>"
"<SomeElement>"
"<AnotherElement>"
"<ElementIWant x="1" y="1"/>"
"<ElementIWant x="2" y="1"/>"
"<ElementIWant x="3" y="1"/>"
"</AnotherElement>"
"</SomeElement>"
"</root>";
boost::property_tree::ptree message;
std::istringstream toParse(test);
boost::property_tree::read_xml(toParse,result_tree);
//Now we need to locate the point elements and set the x/y accordingly.
std::pair< boost::property_tree::ptree::const_assoc_iterator,
boost::property_tree::ptree::const_assoc_iterator > result =
message.equal_range("ElementIWant");
for( boost::property_tree::ptree::const_assoc_iterator it = result.first;
it != result.second; ++it )
{
std::cout << it->first << " : ";
const boost::property_tree::ptree& x = it->second.get_child( "<xmlattr>.x" );
const boost::property_tree::ptree& y = it->second.get_child( "<xmlattr>.y" );
std::cout << x.get_value<int>() << "," << y.get_value<int>() << "n";
}
return;
}
然而,它似乎无法返回节点(我怀疑这是因为equal_range在提供的树节点级别上工作),这使我想到了上面的问题…
不可能直接遍历所有元素;文档说
没有办法遍历整个树。
现在,你可以使用递归,并在每个级别应用STL算法来模拟它;在我下面的示例中,它不符合您在单个循环中完成此操作的要求,但它确实有效:
template <typename InputIt, typename OutputIt, typename Compare>
void collect(InputIt first, InputIt last, OutputIt dest, Compare comp)
{
typedef typename std::iterator_traits<InputIt>::reference reference;
std::copy_if (
first, last, dest,
[comp] (reference what) { return comp(what.first); });
std::for_each (
first, last,
[dest, comp] (reference what) { collect(what.second.begin(), what.second.end(), dest, comp); });
}
std::vector<std::pair<std::string, ptree>> match;
collect(
xml.begin (), xml.end (), std::back_inserter(match),
[] (const std::string& key) { return key == "ElementIWant"; });
for (auto pair: match)
{
std::cout << pair.first << std::endl;
}
这是一个"完全"递归的版本,并保留了外观的顺序:
template <typename InputIt, typename OutputIt, typename Compare>
void collect_recursive(InputIt first, InputIt last, OutputIt dest, Compare comp)
{
typedef typename std::iterator_traits<InputIt>::reference reference;
if (first == last)
{
return;
}
auto begin = first->second.begin ();
auto end = first->second.end ();
if (begin != end)
{
collect_recursive (begin, end, dest, comp);
}
if (comp (first->first))
{
dest = *first;
}
collect_recursive (++first, last, dest, comp);
}
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 迭代时从向量和内存中删除对象
- 如何在c++迭代器类型中包装std::chrono
- 带过滤器的现代迭代c++集合
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- C++矢量迭代
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 擦除while循环中迭代的元素
- 实现一个在集合上迭代的模板函数
- 对于set上的循环-获取next元素迭代器
- 在向量内的向量上迭代
- 为什么output_editor Concept不需要output_e迭代器标记
- 如何使用 ptree(boost) 在 XML 文件中迭代具有相同名称的子项
- Qt c++ QDomDocument,递归迭代XML数据,检索没有子数据的文本元素
- 如何迭代xml文件并将其存储在map中
- boost-ptre如何使用迭代器修改XML
- 如何在boost::property_tree中迭代XML结构