Reputation: 41
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!
Upvotes: 0
Views: 468
Reputation: 2947
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. ;-)
Upvotes: 1