0

I have following xml file. 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. I need to read and be able to compare if bay 1 and bay 2 drives has same firmware versions. I am using Boost(version 1.41 property tree on Red Hat and C++)

<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. So far I can read the xml file but I can not search and see if firmware versions are same. 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. Anyone else have a better idea, like using an iterator? If so please show me how to do that.

usustarr
  • 418
  • 1
  • 5
  • 21
  • Look at this line: `pt &iterationLevel = pt.get_child(L"VGHL.StringTable");` – Ryan Jun 14 '12 at 20:35
  • For starters, `pt` is not a `type` it's a variable name. So `pt& iterationLevel` does not make sense, thats the first compilation error you're getting. Re-read your code, there's a lot of problems. Also you're probably missing includes for `boost`. – Ryan Jun 14 '12 at 21:08
  • Boost.PropertyTree's XML parser is only robust enough to parse the XML that it generates; it is _not_ a general-use XML parser. – ildjarn Jun 14 '12 at 21:18
  • What would be a better solution? – usustarr Jun 15 '12 at 19:43
  • What do you guys think about XML data binding for C++? http://www.codesynthesis.com/products/xsd/ – usustarr Jun 15 '12 at 20:06

1 Answers1

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");
        ...
    }
...
AlexT
  • 1,413
  • 11
  • 11