簡體   English   中英

使用屬性樹在boost中進行xml解析

[英]xml parsing in boost with property tree

我有以下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循環。 其他人有更好的想法,比如使用迭代器? 如果是這樣,請告訴我如何做到這一點。

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