1

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://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.earthtools.org/timezone.xsd">
  <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://www.earthtools.org/webservices.htm, 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.

1 Answer 1

4
const char* localtime = doc.child("timezone").child("localtime").text().get();

or

const char* localtime = doc.child("timezone").child_value("localtime");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.