Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [XML] - Odczyt plików xml
Forum PHP.pl > Forum > Przedszkole
topor1988
Witam.
Potrzebuje odczytac dane z pliku *xml. Dokładnie pola name i file. przeglądałem już manuala, testowałem ale za każdym razem wychodzą jakies błędy. Czy ktoś może pomoc jak odczytac te dany?



  1. <?xml version="1.0"?>
  2. <monsters>
  3.    <monster name="Amazon" file="Amazon.xml" />
  4.    <monster name="Ancient Scarab" file="Ancient scarab.xml" />
  5.    <monster name="Assassin" file="Assassin.xml" />
  6.    .
  7.    .
  8.    .
  9. </monsters>
c3zi
Nie wiem w jaki sposob przegladales tego manuala:
simplexml

Przeczytaj również: attributes()

Ponadto masz rozne rozw. do tego, np. DOM, wspomaniane wyzej simplexml, ...
Mozesz rowniez wykorzystac w obu XPath.
dawik
Masz chyba o to chodzi:
  1. <?
  2. $path = "C:monster";
  3. $folder = opendir($path);
  4. print("<b>Monsters:</b><br>\n<ul>\n");
  5. while($filename = readdir($folder)) {
  6. if($filename == "." || $filename == "..") continue;
  7. $temp = explode(".", $filename);
  8. if($temp[1] == "xml") {
  9. $name = $temp[0];
  10. print("<li><a href=\"index.php?show=monster&name=$name\">$name</a>\n");
  11. }
  12. }
  13. print("\n</ul>");
  14. ?>

W miejsce $path umieśc link do plików .xml
W
  1. <?php
  2. <a href=&#092;"index.php?show=monster&name=$name\">
  3. ?>
umieść co chcesz. To są docelowe informacje pobierane z pliku. Jak chcesz moge ci też w tym pomóc.!
topor1988
Zaglądałem do manuala...

Teoretycznie wszytko działa ładnie gdy xml jest zmienna definiowaną w pliku php, lecz gdy otwieram plik poprzez simplexml_load_file to już wyskakują błędy:

Kod
     Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: Entity: line 202: parser error : Start tag expected, '<' not found in C:\Program Files\WebServ\httpd\tablice\kk.php on line 44
    
     Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: in C:\Program Files\WebServ\httpd\tablice\kk.php on line 44
    
     Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: ^ in C:\Program Files\WebServ\httpd\tablice\kk.php on line 44
    
     Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\Program Files\WebServ\httpd\tablice\kk.php:44 Stack trace: #0 C:\Program Files\WebServ\httpd\tablice\kk.php(44): SimpleXMLElement->__construct('?questionmark.gifquestionmark.gifquestionmark.gifquestionmark.gifquestionmark.gifquestionmark.gifquestionmark.gif...') #1 {main} thrown in C:\Program Files\WebServ\httpd\tablice\kk.php on line 44


Skrypt wyglada następująco:

  1. <?php
  2.  
  3. /* $xml4 = '<?xml version="1.0"?>
  4. <monsters>
  5. <monster name="Banshee" file="Banshee.xml" />
  6. <monster name="Bat" file="Bat.xml" />
  7. <monster name="Bear" file="Bear.xml" />
  8. <monster name="Behemoth" file="Behemoth.xml" />
  9. <monster name="Beholder" file="Beholder.xml" />
  10. <monster name="Black Knight" file="Black Knight.xml" />
  11. <monster name="Black Sheep" file="Black Sheep.xml" />
  12. <monster name="Blue Djinn" file="Blue Djinn.xml" />
  13. <monster name="Bone Beast" file="Bone Beast.xml" />
  14. <monster name="Bug" file="Bug.xml" />>
  15. </monsters>'; */
  16.  
  17.  
  18.  
  19.  
  20. class SimpleXMLElementExtended extends SimpleXMLElement{
  21.  
  22. public function pobierz_at($name){
  23. foreach($this->attributes() as $key=>$val){
  24. if($key == $name){
  25. return (string)$val;
  26. }// end if
  27. }// end foreach
  28. }// end function getAttribute
  29.  
  30.  
  31.  
  32. public function ilosc_potworow(){
  33. $cnt = 0;
  34. foreach($this->children() as $node){
  35. $cnt++;
  36. }// end foreach
  37. return (int)$cnt;
  38. }// end function getChildrenCount
  39.  
  40. }
  41. $plik = 'monsters.xml';
  42. $xml4 = simplexml_load_file($plik);
  43.  
  44. $xml2 = new SimpleXMLElementExtended($xml4);
  45.  
  46. print('<pre>');
  47. print('Ilosc potworkow: '.$xml2->ilosc_potworow());
  48. print('</pre>');
  49.  
  50.  
  51. print('<pre>');
  52. print('Nazwa potworka: '.$xml2->monster[3]->pobierz_at('name'));
  53. print('</pre>');
  54.  
  55. print('<pre>');
  56. print('Plik potworka: '.$xml2->monster[3]->pobierz_at('file'));
  57. print('</pre>');
  58.  
  59. ?>


Sam sobie poradziłem.

W bardziej prosty sposób.

Oto kod dla potomnych:

  1. <?
  2.  echo 'Lista mobow: <br/>';
  3.  
  4.  $moby = simplexml_load_file('monsters.xml');
  5.  
  6.  foreach($moby -> monster as $mob){
  7. echo 'Nazwa potworka :<b>'. $mob['name'] .'</b> ------- Plik: <b>'. $mob['file'] .'</b><br>'; 
  8.  }
  9. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.