C++ 在属性树中访问多值键
我正在尝试查询属性树中的多值键 我已从中引用了参考资料 下面是我的一段Xml:C++ 在属性树中访问多值键,c++,xml,boost,boost-propertytree,C++,Xml,Boost,Boost Propertytree,我正在尝试查询属性树中的多值键 我已从中引用了参考资料 下面是我的一段Xml: <Element type="MyType"> <Name type="Number"> <KeyFrame time="0">1920</KeyFrame> <KeyFrame time="3000">1080</KeyFrame> <KeyFrame tim
<Element type="MyType">
<Name type="Number">
<KeyFrame time="0">1920</KeyFrame>
<KeyFrame time="3000">1080</KeyFrame>
<KeyFrame time="4000">720</KeyFrame>
</Name>
</Element>
1920
1080
720
代码如下:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <sstream>
#include <iostream>
using boost::property_tree::ptree;
int main() {
std::stringstream ss("<Element type=\"MyType\">"
"<Name type=\"Number\">"
"<KeyFrame time=\"0\">1920</KeyFrame>"
"<KeyFrame time=\"3000\">1080</KeyFrame>"
"<KeyFrame time=\"4000\">720</KeyFrame></Name>"
"</Element>");
ptree pt;
boost::property_tree::read_xml(ss, pt);
auto& root = pt.get_child("Element");
for (auto& child : root.get_child("Name"))
{
if(child.first == "KeyFrame")
{
std::cout<<child.second.get<int>("<xmlattr>.time", 0)<<" : "<<child.second.data()<<std::endl;
}
}
}
#包括
#包括
#包括
#包括
使用boost::property_tree::ptree;
int main(){
std::stringstream ss(“”)
""
"1920"
"1080"
"720"
"");
ptree-pt;
boost::property_tree::read_xml(ss,pt);
auto&root=pt.get_子元素(“元素”);
for(auto&child:root.get_child(“Name”))
{
if(child.first==“关键帧”)
{
std::cout我建议获取值
:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
#include <sstream>
std::string const sample = R"(<Element type="MyType">
<Name type="Number">
<KeyFrame time="0">1920</KeyFrame>
<KeyFrame time="3000">1080</KeyFrame>
<KeyFrame time="4000">720</KeyFrame>
</Name>
</Element>)";
using boost::property_tree::ptree;
int main() {
std::stringstream ss(sample);
ptree pt;
boost::property_tree::read_xml(ss, pt);
auto &root = pt.get_child("Element");
for (auto &child : root.get_child("Name")) {
if (child.first == "KeyFrame") {
auto node = child.second;
std::cout << node.get<int>("<xmlattr>.time", 0) << " : "
<< node.get_value<int>() << std::endl;
}
}
}
0 : 1920
3000 : 1080
4000 : 720