0

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!

mvk
  • 41
  • 1
  • 6
  • There are quite a few XML loaders and parsers available, many of them for C++. You might want to search a little to find a few of those libraries. – Some programmer dude Jun 04 '19 at 10:28
  • I think you can look at [this](https://stackoverflow.com/questions/9387610/what-xml-parser-should-i-use-in-c). But you could have found this link by yourself :) – Fareanor Jun 04 '19 at 10:30
  • You can try this [Reading structures from XML](https://github.com/incoder1/IO/wiki/Reading-structures-from-XML) – Victor Gubin Jun 04 '19 at 10:35

1 Answers1

1

If you want to stick to boost::propery_tree and have no need to understand (and parse) more XML, maybe the following stackoverflow answer will help you: How are attributes parsed in Boost.PropertyTree?

Your new format uses XML attributes whereas your old format used only XML elements. You don't need to know everything. But you need to know the technical terms (like attribute), so you can google like I did. ;-)

ur.
  • 2,890
  • 1
  • 18
  • 21
  • Thanks a lot, I wasn't familiar with the terms attributes and elements. I found the answer in the link you attached. Thanks! – mvk Jun 04 '19 at 11:37