mardi 4 juin 2019

how to parse XML with ParameterName and ParameterValue using c++

I'm receiving an xml file with credentials and I need to parse it's values in c++11. The problem is that I wasn't able to parse this specific xml format (format 1):

<Parameters>
    <Parameter ParameterName="AccessKey" ParameterValue="ABC"/> 
    <Parameter ParameterName="SecretKey" ParameterValue="XYZ"/> 
</Parameters>

I am familiar with boost::property_tree but I was able to parse only the format below (format 2):

<Parameters>
    <AccessKey>ABC</AccessKey>
    <SecretKey>XYZ</SecretKey>
</Parameters>

Below is the code I used to parse the xml format 2:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
namespace pt = boost::property_tree;

bool getCredentialsfromXml(const std::string &xmlFileName, Credentials& credentials)
{   
    pt::ptree tree;
    pt::read_xml(xmlFileName, tree);

    // 1. AccessKey
    credentials.m_accessKey = tree.get_optional<std::string>("Parameters.AccessKey");

    // 2. SecretKey
    credentials.m_secretKey = tree.get_optional<std::string>("Parameters.SecretKey");

    return true;
}

Is there any way to modify my code to parse xml format 1? or any other way to parse xml format 1 in c++11?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire