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