0

这是我的代码:

<?php
$dom = new DOMDocument();
$dom->load('http://services.gisgraphy.com/geoloc/search?lat=22.298569900000000000&lng=70.794301799999970000&radius=7000', true);
$dom->save('filename.xml');
?>

我得到这种类型的输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<results xmlns="http://gisgraphy.com">
    <numFound>1</numFound>
    <QTime>67</QTime>
    <result>
        <distance>1139.81967842778</distance>
        <name>Rājkot</name>// 
        <adm1Code>09</adm1Code>
        <adm1Name>State of Gujarāt</adm1Name>
        <asciiName>Rajkot</asciiName>
        <countryCode>IN</countryCode>
        <featureClass>P</featureClass>
        <featureCode>PPL</featureCode>
        <featureId>1258847</featureId>
        <gtopo30>139</gtopo30>
        <population>1177362</population>
        <timezone>Asia/Kolkata</timezone>
        <lat>22.299999237060547</lat>
        <lng>70.78333282470703</lng>
        <placeType>City</placeType>
        <oneWay>false</oneWay>
        <length>0.0</length>
        <google_map_url>http://maps.google.com/maps?f=q&amp;amp;ie=UTF-8&amp;amp;iwloc=addr&amp;amp;om=1&amp;amp;z=12&amp;amp;q=R%C4%81jkot&amp;amp;ll=22.329999237060548,70.78333282470703</google_map_url>
        <yahoo_map_url>http://maps.yahoo.com/broadband?mag=6&amp;amp;mvt=m&amp;amp;lon=70.78333282470703&amp;amp;lat=22.299999237060547</yahoo_map_url>
        <country_flag_url>/images/flags/IN.png</country_flag_url>
    </result>
</results>

在上面的 XML 文件中,我想将name节点值中的特殊字符转换为简单字符,例如Rājkot包含ā我想转换为简单字符的特殊a字符。

4

1 回答 1

1

下面的代码使用 SimpleXML 扩展循环遍历每个元素,并通过将字符集转换为 UTF-8 来result修改其中元素的文本内容。name

<?php
  $results = new SimpleXMLElement('http://services.gisgraphy.com/geoloc/search?lat=22.298569900000000000&lng=70.794301799999970000&radius=7000', NULL, TRUE);
  foreach($results->result as $result) {
    $result->name = iconv('utf-8', 'ascii//TRANSLIT', $result->name);
  }
  $results->asXML('results_simple.xml');
?>

以下是使用 DOMDocument 而不是 SimpleXML 的上述代码的替代版本...

<?php
  $doc = new DOMDocument();
  $doc->load('http://services.gisgraphy.com/geoloc/search?lat=22.298569900000000000&lng=70.794301799999970000&radius=7000');
  // retrieve all elements with a tag name of "name"
  $names = $doc->getElementsByTagName('name');
  foreach($names as $name) {
    $name->nodeValue = iconv('utf-8', 'ascii//TRANSLIT', $name->nodeValue);
  }
  $doc->save('results_dom.xml');     
?>

最后,此代码使用 DOMDocument 递归遍历 XML 数据中的所有元素/节点,将转换应用于每个文本节点的值...

<?php
  function convertNodeValueChars($node) {
    if ($node->hasChildNodes()) {
      foreach ($node->childNodes as $childNode) {
        if ($childNode->nodeType == XML_TEXT_NODE) {
          $childNode->nodeValue = iconv('utf-8', 'ascii//TRANSLIT', $childNode->nodeValue);
        }
        convertNodeValueChars($childNode);         
      }
    }      
  } 

  $doc = new DOMDocument();
  $doc->load('http://services.gisgraphy.com/geoloc/search?lat=22.298569900000000000&lng=70.794301799999970000&radius=7000');
  convertNodeValueChars($doc->documentElement);
  $doc->save('results_dom.xml');     
?>

您在此处发布之前是否搜索过类似的问题?

我通过简单的 Google 搜索php edit xml element value发现了许多相关问题...

为了转换字符,看看这个建议......

于 2013-03-25T10:28:13.827 回答