在XML节点值中将特殊字符转换为简单字符并另存为XML文件

时间:2013-03-25 09:37:18

标签: php xml xml-parsing transliteration

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

下面的代码使用SimpleXML扩展来遍历每个result元素,并通过执行字符集转换为UTF-8来修改其中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 找到了一些相关的问题......

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