As an example I am using the XML file listed here:
https://msdn.microsoft.com/en-us/library/ms256129(v=vs.110).aspx
The XML File:
<?xml version="1.0"?>
<purchaseOrder xmlns="http://tempuri.org/po.xsd" orderDate="1999-10-20">
<shipTo country="US">
<name>Alice Smith</name>
<street>123 Maple Street</street>
<city>Mill Valley</city>
<state>CA</state>
<zip>90952</zip>
</shipTo>
<billTo country="US">
<name>Robert Smith</name>
<street>8 Oak Avenue</street>
<city>Old Town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<comment>Hurry, my lawn is going wild!</comment>
<items>
<item partNum="872-AA">
<productName>Lawnmower</productName>
<quantity>1</quantity>
<Price>
<USPrice>148.95</USPrice>
<UKPrice>150.02</UKPrice>
</Price>
<comment>Confirm this is electric</comment>
</item>
<item partNum="926-AA">
<productName>Baby Monitor</productName>
<quantity>1</quantity>
<Price>
<USPrice>39.95</USPrice>
<UKPrice>37.67</UKPrice>
</Price>
<USPrice>39.98</USPrice>
<shipDate>1999-05-21</shipDate>
</item>
</items>
</purchaseOrder>
Currently I am using the following code but using that I am able to read only one child node that is purchaseOrder.shipTo country only. how to read till tag USPrice? Does boost xml parser support 4 level tag value parsing?
const std::string XML_PATH1 = "./test1.xml";
#define ROOTTAG "purchaseOrder"
boost::property_tree::ptree pt;
boost::property_tree::read_xml( XML_PATH1, pt);
BOOST_FOREACH(boost::property_tree::ptree::value_type & v, pt.get_child(ROOTTAG)){
xmlmap[v.first.data()] = v.second.data();
}
I want to read and store as follows in the xmlmap <string, string>
.
map key = items.item partNum.USPrice
map value = 39.98 (post converting to string)
Update:
I tried the following but it is giving me compilation error as
error: ‘boost::property_tree::ptree’ has no member named ‘second’
boost::property_tree::ptree lt = subtree.second;
Code:
const std::string XML_PATH1 = "./test1.xml";
#define ROOTTAG "purchaseOrder"
boost::property_tree::ptree pt1;
boost::property_tree::read_xml( XML_PATH1, pt1);
BOOST_FOREACH(boost::property_tree::ptree::value_type & node, pt1.get_child(ROOTTAG))
{
std::string tagname = node.first;
tagname += ".";
boost::property_tree::ptree subtree = node.second;
BOOST_FOREACH( boost::property_tree::ptree::value_type & v, subtree.get_child(node.first.data()))
{
boost::property_tree::ptree lt = subtree.second;
tagname += v.first.data();
tagname += ".";
BOOST_FOREACH( boost::property_tree::ptree::value_type & vt, lt.get_child(v.first.data()))
{
std::string name1 = vt.first.data();
tagname += name1;
if(name1 != "<xmlattr>") {
std::string tagvalue = lt.get<std::string>(name1);
tagname += name1;
xmlmap[tagname] = tagvalue;
}
}
}
}