Domanda

This is my code:

<?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');
?>

I am getting this type of output:

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

In the above XML file I want to convert special characters in the name node value to simple characters e.g. Rājkot contains the special character ā which I would like to convert to a simple a character.

È stato utile?

Soluzione

The code below uses the SimpleXML extension to loop through each result element and modifies the text content of the name element within it by performing a character set conversion to UTF-8.

<?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');
?>

The following is an alternative version of the above code using DOMDocument instead of 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');     
?>

Finally, this code uses DOMDocument to recursively traverse through all elements/nodes in the XML data, applying the conversion to the value of each text node...

<?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');     
?>

Did you search for similar questions before posting here?

I found a number of relevant questions with a simple Google search for php edit xml element value...

In order to convert characters, have a look at this suggestion...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top