Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> ustawienie kodowania XML przy użyciu PHP
sunpietro
post
Post #1





Grupa: Zarejestrowani
Postów: 262
Pomógł: 26
Dołączył: 23.01.2009
Skąd: eZ Systems

Ostrzeżenie: (0%)
-----


witam,
mam skrypt PHP, który zczytuje dane z bazy do XML, które następnie są wstawiane na stronie przy użyciu AJAXa.
Problem polega na tym, że XML źle wyświetla znaki. Fizycznie na serwerze nie istnieje żaden plik XML, jest on generowany "w locie".
Jak można ustawić kodowanie XML na utf-8?
Baza danych, pliki skryptów PHP i JS mają kodowanie utf-8.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
sunpietro
post
Post #2





Grupa: Zarejestrowani
Postów: 262
Pomógł: 26
Dołączył: 23.01.2009
Skąd: eZ Systems

Ostrzeżenie: (0%)
-----


OK, oto plik zczytujący dane z bazy i przerabiający na XML:
  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');
  3. require("db.php");
  4.  
  5. function parseToXML($htmlStr)
  6. {
  7. $xmlStr=str_replace('<','&lt;',$htmlStr);
  8. $xmlStr=str_replace('>','&gt;',$xmlStr);
  9. $xmlStr=str_replace('"','&quot;',$xmlStr);
  10. $xmlStr=str_replace("'",'&apos;',$xmlStr);
  11. $xmlStr=str_replace("&",'&amp;',$xmlStr);
  12. return $xmlStr;
  13. }
  14. $connection=mysql_connect (localhost, $username, $password);
  15. if (!$connection) {
  16. die('Nie polaczono z serwerem baz danych: '. mysql_error());
  17. }
  18. $db_selected = mysql_select_db($database, $connection);
  19. if (!$db_selected) {
  20. die ('Nie mozna polaczyc sie z baza danych: '. mysql_error());
  21. }
  22. $query = "SELECT * FROM google_maps WHERE 1";
  23. $result = mysql_query($query);
  24. if (!$result) {
  25. die('Niepoprawne zapytanie do bazy: '. mysql_error());
  26. }
  27.  
  28. <znaczniki>';
  29. while ($row = @mysql_fetch_assoc($result)){
  30. echo '<znacznik ';
  31. echo 'name="' . parseToXML($row['nazwa']) . '" ';
  32. echo 'address="' . parseToXML($row['adres']) . '" ';
  33. echo 'lat="' . $row['lat'] . '" ';
  34. echo 'lng="' . $row['lng'] . '" ';
  35. echo 'type="' . $row['typ'] . '" ';
  36. echo '/>';
  37. }
  38. echo '</znaczniki>';
  39.  
  40. ?>


oraz kod pliku js wywołujący dane z XML do mapy google.
  1. <script type="text/javascript">
  2. //<![CDATA[
  3.  
  4. var customIcons = {
  5. restaurant: {
  6. icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
  7. shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  8. },
  9. bar: {
  10. icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
  11. shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  12. }
  13. };
  14.  
  15. function load() {
  16. var map = new google.maps.Map(document.getElementById("map"), {
  17. center: new google.maps.LatLng(50.286264, 19.104079),
  18. zoom: 13,
  19. mapTypeId: 'roadmap'
  20. });
  21. var infoWindow = new google.maps.InfoWindow;
  22.  
  23. // Change this depending on the name of your PHP file
  24. downloadUrl("phpsqlajax_genxml.php", function(data) {
  25. var xml = parseXml(data);
  26. var markers = xml.documentElement.getElementsByTagName("znacznik");
  27. for (var i = 0; i < markers.length; i++) {
  28. var name = markers[i].getAttribute("name");
  29. var address = markers[i].getAttribute("address");
  30. var type = markers[i].getAttribute("type");
  31. var point = new google.maps.LatLng(
  32. parseFloat(markers[i].getAttribute("lat")),
  33. parseFloat(markers[i].getAttribute("lng")));
  34. var html = "<b>" + name + "</b> <br/>" + address;
  35. var icon = customIcons[type] || {};
  36. var marker = new google.maps.Marker({
  37. map: map,
  38. position: point,
  39. icon: icon.icon,
  40. shadow: icon.shadow
  41. });
  42. bindInfoWindow(marker, map, infoWindow, html);
  43. }
  44. });
  45. }
  46.  
  47. function bindInfoWindow(marker, map, infoWindow, html) {
  48. google.maps.event.addListener(marker, 'click', function() {
  49. infoWindow.setContent(html);
  50. infoWindow.open(map, marker);
  51. });
  52. }
  53.  
  54. function downloadUrl(url, callback) {
  55. var request = window.ActiveXObject ?
  56. new ActiveXObject('Microsoft.XMLHTTP') :
  57. new XMLHttpRequest;
  58.  
  59. request.onreadystatechange = function() {
  60. if (request.readyState == 4) {
  61. request.onreadystatechange = doNothing;
  62. callback(request.responseText, request.status);
  63. }
  64. };
  65.  
  66. request.open('GET', url, true);
  67. request.send(null);
  68. }
  69.  
  70. function parseXml(str) {
  71. if (window.ActiveXObject) {
  72. var doc = new ActiveXObject('Microsoft.XMLDOM');
  73. doc.loadXML(str);
  74. return doc;
  75. } else if (window.DOMParser) {
  76. return (new DOMParser).parseFromString(str, 'text/xml');
  77. }
  78. }
  79.  
  80. function doNothing() {}
  81.  
  82. //]]>


no i oprócz tego dorzucam pliki z Google Maps API
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 11.10.2025 - 21:43