Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [XML] - Odczyt plików xml
topor1988
post
Post #1





Grupa: Zarejestrowani
Postów: 59
Pomógł: 0
Dołączył: 2.03.2008

Ostrzeżenie: (10%)
X----


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>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
topor1988
post
Post #2





Grupa: Zarejestrowani
Postów: 59
Pomógł: 0
Dołączył: 2.03.2008

Ostrzeżenie: (10%)
X----


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('?(IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.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. ?>
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: 26.09.2025 - 22:13