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('?







...') #1 {main} thrown in C:\Program Files\WebServ\httpd\tablice\kk.php on line 44
Skrypt wyglada następująco:
<?php
/* $xml4 = '<?xml version="1.0"?>
<monsters>
<monster name="Banshee" file="Banshee.xml" />
<monster name="Bat" file="Bat.xml" />
<monster name="Bear" file="Bear.xml" />
<monster name="Behemoth" file="Behemoth.xml" />
<monster name="Beholder" file="Beholder.xml" />
<monster name="Black Knight" file="Black Knight.xml" />
<monster name="Black Sheep" file="Black Sheep.xml" />
<monster name="Blue Djinn" file="Blue Djinn.xml" />
<monster name="Bone Beast" file="Bone Beast.xml" />
<monster name="Bug" file="Bug.xml" />>
</monsters>'; */
class SimpleXMLElementExtended extends SimpleXMLElement{
public function pobierz_at($name){
foreach($this->attributes() as $key=>$val){
if($key == $name){
return (string)$val;
}// end if
}// end foreach
}// end function getAttribute
public function ilosc_potworow(){
$cnt = 0;
foreach($this->children() as $node){
$cnt++;
}// end foreach
return (int)$cnt;
}// end function getChildrenCount
}
$plik = 'monsters.xml';
$xml4 = simplexml_load_file($plik);
$xml2 = new SimpleXMLElementExtended($xml4);
print('Ilosc potworkow: '.$xml2->ilosc_potworow());
print('Nazwa potworka: '.$xml2->monster[3]->pobierz_at('name'));
print('Plik potworka: '.$xml2->monster[3]->pobierz_at('file'));
?>
Sam sobie poradziłem.
W bardziej prosty sposób.
Oto kod dla potomnych:
<?
echo 'Lista mobow: <br/>';
$moby = simplexml_load_file('monsters.xml');
foreach($moby -> monster as $mob){
echo 'Nazwa potworka :<b>'. $mob['name'] .'</b> ------- Plik: <b>'. $mob['file'] .'</b><br>'; }
?>