0

我有以下 xml 文件。它显示了 2 个驱动器(托架 1 和托架 2)的固件版本信息。此时,除了托架 1 和托架 2 外,两个驱动器的一切看起来都相似。但我希望它们具有不同的固件版本。我需要阅读并能够比较托架 1 和托架 2 驱动器是否具有相同的固件版本。我正在使用 Boost(Red Hat 和 C++ 上的 1.41 版属性树)

<Node>
   <Type>XET</Type>
   <Reference>00110232</Reference>
   <Date>02-09-2012</Date>
</Node>
<Node>
   <Type>XET</Type>
   <Reference>000000</Reference>
   <Date>02-09-2012</Date>
</Node>

我的 C++ 不是那么好,关于 Boost 的文档真的很烂。到目前为止,我可以读取 xml 文件,但无法搜索并查看固件版本是否相同。我尝试了几种不同的方法,但运气不佳。我真的很感激这方面的一些帮助。

  #include <boost/property_tree/ptree.hpp>
  #include <boost/property_tree/xml_parser.hpp>


  using boost::property_tree::ptree;
  ptree pt;

  // reading file.xml
  read_xml(xmlFilePath, pt, boost::property_tree::xml_parser::trim_whitespace );

  string c ;
  try
  {
    c = pt.get<string>("Node.Reference");
  }
  catch(std::exception const& e)
  {
    std::cout << e.what() << std::endl;
  }

  cout << "value is: " << c << endl;

我解决了一些问题。有人可以告诉我如何获取所有节点吗?此代码当前找到第一个匹配项并打印它,仅此而已。如果我知道将有多少节点,我可以使用 for 循环。其他人有更好的主意,比如使用迭代器吗?如果是这样,请告诉我如何做到这一点。

4

1 回答 1

3
...
#include <boost/foreach.hpp>
...

namespace PT = boost::property_tree;


...
    read_xml(xmlFilePath, pt, boost::property_tree::xml_parser::trim_whitespace );

    BOOST_FOREACH(const PT::ptree::value_type& node, pt.get_child("Node"))
    {
        PT::ptree& nodeTree = node.second;
        const std::string type      = nodeTree.get<std::string>("Type");
        const std::string reference = nodeTree.get<std::string>("Reference");
        const std::string date      = nodeTree.get<std::string>("Date");
        ...
    }
...
于 2013-09-13T06:24:53.073 回答