0

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.

w5m
  • 2,286
  • 3
  • 34
  • 46
jack lanza
  • 299
  • 3
  • 18
  • 1
    Please show the input XML data and the output XML format you would like – DaveRandom Mar 25 '13 at 09:58
  • 2
    Maybe I'm missing something, but why do you convert XML into array if you actually need it as XML? – ulentini Mar 25 '13 at 09:58
  • @Uby you are right this link also give me xml file but i want to save this xml file and in them there is one node with name`name` there value is `Rajkot` this value content some special character like in `Rajkot` `a` check it... – jack lanza Mar 25 '13 at 10:02
  • @Uby i want to this link output using `php` its possible then forget all above – jack lanza Mar 25 '13 at 10:03
  • @DaveRandom same as click on link... but its good for me if you solve my one query on given url there have one node with `name` its value is `Rajkot` this value have one special character `a` i want to convert in to character... but dont forget my own question save given url output as a xml if dont need convert in to array then good... – jack lanza Mar 25 '13 at 10:05
  • So am I right in saying that you wish to: (a) read in XML data from a URL, (b) change the value of the `name` element and (c) save the modified XML data to file? – w5m Mar 25 '13 at 10:11
  • This question has already been asked before: http://stackoverflow.com/a/2956678/2047725 – w5m Mar 25 '13 at 10:14
  • @w5m i got the solution for save the xml from url but now what for modified `name` – jack lanza Mar 25 '13 at 10:16
  • @w5m check my modification of question... – jack lanza Mar 25 '13 at 10:20
  • 2
    Instead of doing guessed char conversions like `Rājkot` to `Rajkot` or `Gujarāt` to `Gujarat`; create a alias table with a `standard_name` column and aliases,`1:n` and keep on updating it. Use char conversion tools (`iconv`) for guess work and manual work for confirmation every now and then. As much phonetics/multiple-languages support is a feature, it's a burden too! – जलजनक Mar 25 '13 at 10:36

1 Answers1

1

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

Community
  • 1
  • 1
w5m
  • 2,286
  • 3
  • 34
  • 46
  • good but i have lots of node those contains special character like `Gujarāt` i think you dont mark this??? – jack lanza Mar 25 '13 at 10:33
  • so what for this every where is `NewName` is not possible so we need something find `ā` and conver into `a` not full name... – jack lanza Mar 25 '13 at 10:37
  • What I posted was an example of how to change the `name` element in each `result` element. It is not difficult to find suggestions on how to convert characters - see my edit. – w5m Mar 25 '13 at 10:39
  • I've modified my code sample to include the suggestion to convert the character. – w5m Mar 25 '13 at 10:53
  • only one node value but not for all only work `name` node b'coz we set in to foreach... – jack lanza Mar 25 '13 at 11:04
  • If you are saying that you want this character conversion to be applied to other elements within the `result` element then include additional lines within the `foreach` loop; one for each appropriate element e.g. `$result->adm1Name`. It only looks relevant to apply this to the text content of 2 or 3 of the elements within `result`. – w5m Mar 25 '13 at 11:09
  • yes its work temporary but we dont like this type of code want to find solution for this... – jack lanza Mar 25 '13 at 11:37
  • 1
    I've added a code example which recursively traverses through all nodes in the data and applies the conversion to the values of text nodes. – w5m Mar 25 '13 at 12:55