【问题标题】:Extracting XML data to php [duplicate]将 XML 数据提取到 php [重复]
【发布时间】:2013-06-04 21:32:45
【问题描述】:

我正在尝试从 XML 文件 (http://freegeoip.net/xml/google.com) 中提取数据。您可以看到文件的内容类似于:

<Response>
<Ip>74.125.235.3</Ip>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>CA</RegionCode>
<RegionName>California</RegionName>
<City>Mountain View</City>
<ZipCode>94043</ZipCode>
<Latitude>37.4192</Latitude>
<Longitude>-122.0574</Longitude>
<MetroCode>807</MetroCode>
<AreaCode>650</AreaCode>
</Response>

我想获取存储在&lt;latitude&gt;&lt;longitude&gt; 标签中的信息,并将它们存储在单独的变量中。问题是,我不知道该怎么做,想知道是否有人可以告诉我如何用 php 解析 XML 文件?

【问题讨论】:

    标签: php xml latitude-longitude geoip


    【解决方案1】:
    $string_data = "<your xml response>";
    $xml = simplexml_load_string($string_data);
    $latitude = (string) $xml->Latitude;
    $longitude = (string) $xml->Longitude;
    echo $latitude.' '.$longitude;
    

    【讨论】:

      【解决方案2】:

      很简单,使用 PHP 的 SimpleXML 库:

      $xml = simplexml_load_file("http://freegeoip.net/xml/google.com");
      echo $xml->Ip; // 173.194.38.174
      echo $xml->CountryCode; // US
      echo $xml->ZipCode; // 94043
      // etc...
      

      【讨论】:

        【解决方案3】:

        PHP 手册有一整节关于 PHP 解析:

        为简单起见,您也可以使用xml_parse_into_struct()

        这是一个很好的例子,使用 SimpleXML:

        http://blog.teamtreehouse.com/how-to-parse-xml-with-php5

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-07
          • 1970-01-01
          • 2015-06-30
          • 1970-01-01
          相关资源
          最近更新 更多