【问题标题】:Parse XML using pugixml使用 pugixml 解析 XML
【发布时间】:2014-07-20 09:36:21
【问题描述】:

我正在尝试使用 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>

请帮我解决这个问题。

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

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

【问题讨论】:

    标签: c++ xml pugixml


    【解决方案1】:
    const char* localtime = doc.child("timezone").child("localtime").text().get();
    

    或

    const char* localtime = doc.child("timezone").child_value("localtime");
    

    【讨论】:

      猜你喜欢
      • 2011-09-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多