Sunday, 20 July 2014

Parse XML using pugixml



I am trying to fetch, internet time using a web service, which provides me an xml. Now, I am trying to parse the xml file using pugixml. The XML returned



<?xml version="1.0" encoding="ISO-8859-1" ?>
<timezone xmlns:xsi="http://ift.tt/ra1lAU" xsi:noNamespaceSchemaLocation="http://ift.tt/1nr2OnO">
<version>1.0</version>
<location>
<latitude>22.5667</latitude>
<longitude>88.3667</longitude>
</location>
<offset>5.5</offset>
<suffix>E*</suffix>
<localtime>20 Jul 2014 14:48:10</localtime>
<isotime>2014-07-20 14:48:10 +0530</isotime>
<utctime>2014-07-20 09:18:10</utctime>
<dst>Unknown</dst>
</timezone>


The way I am trying to parse it.



pugi::xml_document doc;
if (!doc.load_file("time.xml")) return -1;

pugi::xml_node tools = doc.child("timezone").child("localtime");

//[code_traverse_iter
for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it)
{
std::cout << "Tool:";

for (pugi::xml_attribute_iterator ait = it->attributes_begin(); ait != it->attributes_end(); ++ait)
{
std::cout << " " << ait->name() << "=" << ait->value();
}

std::cout << std::endl;
}

return 0;


I need fetch the value of this node



<localtime>20 Jul 2014 14:48:10</localtime>


Please help me get through this.


P.S: The web-service I am using can be found http://ift.tt/1rAVdCp, hope it helps someone.


I know I can do a simple file operation to fetch the data as the xml is not that long, but still I would like to use the parser.


No comments:

Post a Comment