简体   繁体   English

使用属性树在boost中进行xml解析

[英]xml parsing in boost with property tree

I have following xml file. 我有以下xml文件。 It is showing firmware version info for 2 drives, bay 1 and bay 2. At this point, everything looks similar for both drives, except bay 1 and bay 2. But I expect these to have different firmware version. 它显示了2个驱动器,托架1和托架2的固件版本信息。此时,除了托架1和托架2之外,两个驱动器的一切看起来都相似。但我希望它们具有不同的固件版本。 I need to read and be able to compare if bay 1 and bay 2 drives has same firmware versions. 我需要阅读并能够比较托架1和托架2驱动器是否具有相同的固件版本。 I am using Boost(version 1.41 property tree on Red Hat and C++) 我正在使用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>

My C++ isnt all that great and documentation on Boost really sucks. 我的C ++并不是那么棒,关于Boost的文档真的很糟糕。 So far I can read the xml file but I can not search and see if firmware versions are same. 到目前为止,我可以读取xml文件,但我无法搜索并查看固件版本是否相同。 I tried couple of different things without much luck. 没有太多运气,我尝试了几种不同的东西。 I would really appreciate some help with this. 我真的很感激这方面的一些帮助。

  #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;

I got some of my issues solved. 我解决了一些问题。 Can someone please tell me how to get all nodes? 有人可以告诉我如何获取所有节点? This code currently finds the 1st match and print it, nothing else. 此代码当前找到第一个匹配并打印它,没有别的。 If I knew how many nodes are going to be there, I could have used a for loop. 如果我知道将有多少节点,我可以使用for循环。 Anyone else have a better idea, like using an iterator? 其他人有更好的想法,比如使用迭代器? If so please show me how to do that. 如果是这样,请告诉我如何做到这一点。

...
#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");
        ...
    }
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM