jeudi 27 mai 2021

How to read specific value from the xml file using boost xml in C++?

Using below xml message format, I am trying to read value of the key "zip code". i.e, "90952"

<list>
<address>
    <key>Name</key>
    <string>Alice Smith</string>
    <key>state</key>
    <string>CA</string>
    <key>zip code</key>
    <string>90952</string>
</address>
</list>

I am trying it like as shown below. Could someone help me how to proceed with this to get the value of the zip code and store it into a string:

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

using namespace std;

const std::string XML_PATH1 = "C://Project//JSONSampleApp//test1.xml";

int main()
{
    boost::property_tree::ptree pt1;
    boost::property_tree::read_xml(XML_PATH1, pt1);
 
 BOOST_FOREACH(boost::property_tree::ptree::value_type const& node, pt1.get_child("list"))
    {
         if (node.first == "address")
         {
             std::string test1 = node.second.get<std::string>("key");
         }
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire