1

我正在尝试使用 Web 服务获取互联网时间,该服务为我提供了一个 xml。现在,我正在尝试使用 pugixml 解析 xml 文件。返回的 XML

<?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>

我试图解析它的方式。

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;

我需要获取这个节点的值

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

请帮助我度过难关。

PS:我正在使用的网络服务可以在http://www.earthtools.org/webservices.htm找到,希望对某人有所帮助。

我知道我可以做一个简单的文件操作来获取数据,因为 xml 并不长,但我仍然想使用解析器。

4

1 回答 1

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

或者

const char* localtime = doc.child("timezone").child_value("localtime");
于 2014-07-20T22:33:59.773 回答