Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Problem ze zmianną globalną. Parsing SAX (XML)
mtrpoland
post
Post #1





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 3.09.2009

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


Witajcie,
natrafiłem na dziwną sytuację. Już wyczerpałem wszelkie pomysły na rozwiązanie.

Parsuje SAX'em plik o strukurze:
  1. <?xml version="1.0" encoding='UTF-8'?>
  2. <PictureCollection>
  3. <MainPath>ble</MainPath>
  4. <Picture>
  5. <ID></ID>
  6. <PATH></PATH>
  7. <DESC></DESC>
  8. </Picture>
  9. <Picture>
  10. <ID></ID>
  11. <PATH></PATH>
  12. <DESC></DESC>
  13. </Picture>
  14. </PictureCollection>


Potrzebuję wydobyć ścieżkę i opis obrazka o konkretnym ID.
Napisałem funkcje:
  1. // FUNKCJE SLUZACE DO WCZYTYWANIA DANYCH KONKRETNEGO OBRAZKU
  2. function dataHandler($parser, $data) {
  3. global $currentTag;
  4. global $currentPID;
  5. global $insideTargetPicture;
  6. global $picPath;
  7. global $picDesc;
  8. if(0==strcmp($currentTag,"ID"))
  9. if($data==$currentPID)
  10. $insideTargetPicture = 1;
  11. if((0==strcmp($currentTag,"PATH"))&&$insideTargetPicture)
  12. $picPath = $data;
  13. if((0==strcmp($currentTag,"DESC"))&&$insideTargetPicture)
  14. $picDesc = $data;
  15. }
  16. function startElement($parser, $name, array $attr) {
  17. global $currentTag;
  18. $currentTag = $name;
  19. }
  20. function endElement($parser, $name) {
  21. global $insideTargetPicture;
  22. if(0==strcmp($name,"PICTURE"))
  23. $insideTargetPicture = 0;
  24. }
  25.  
  26. function fillPictureData($id, &$url, &$desc) {
  27. global $currentPID;
  28. global $currentTag;
  29. global $insideTargetPicture;
  30. global $picPath;
  31. global $picDesc;
  32. $currentPID = $id;
  33. $insideTargetPicture = 0;
  34. $file = "./data/welcomePictures.xml";
  35. $parser = xml_parser_create();
  36. xml_set_element_handler($parser, "startElement", "endElement");
  37. xml_set_character_data_handler($parser, "dataHandler");
  38.  
  39. if (!($fp = fopen($file, "r")))
  40. die("could not open XML input");
  41.  
  42. while ($data = fread($fp, 4096))
  43. if(!xml_parse($parser, $data, feof($fp)))
  44. die(sprintf("XML error: %s at line %d",
  45. xml_error_string(xml_get_error_code($parser)),
  46. xml_get_current_line_number($parser)));
  47.  
  48. $url = $picPath;
  49. $desc = $picDesc;
  50. xml_parser_free($parser);
  51. }


Problem jest natury "autozerującej" się zmiennej globalnej
  1. $picPath
.
Zmienna jest ładnie wypełniana w
  1. dataHandler
w którym odczytywana jest pożądana wartość.
Jednak po zakończeniu parsowania, zmienna jest już pusta, mimo że w kodzie zmienia się tylko raz w
  1. dataHandler
.

Jakieś pomysły?
:-)
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: 22.08.2025 - 17:28