Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在属性树中访问多值键_C++_Xml_Boost_Boost Propertytree - Fatal编程技术网

C++ 在属性树中访问多值键

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

我正在尝试查询属性树中的多值键

我已从中引用了参考资料

下面是我的一段Xml:

<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