parks
parks

Reputation: 35

XML Parse Attributes PHP $string = value ;

I have xml. It's movie showtimes, and i try parse.

<shows>
    <show id="160575" film_id="7043" cinema_id="89" hall_id="241">
        <begin>2012-11-15</begin>
        <end>2012-11-18</end>
        <times>
            <time time="10:30:00">
                <prices>20, 30</prices>
                <note><![CDATA[]]></note>
            </time>
        </times>
    </show>
</shows>

I parse my XML and echo content : String "film_id_m" must be 7043 from the tag .

$xmlstr = file_get_contents('test.xml');
$x = new SimpleXMLElement($xmlstr); 
$id_f = 89;
$cinema_id = "//show[@cinema_id=".$id_f."]";

$cinema=$x->xpath($cinema_id);

///////////string///////////////////
$begin_m = $cinema[0]->begin;
$end_m = $cinema[0]->end;
$film_id_m = ????????; 



/////////echo//////////////////////
echo "<b>BEGIN: </b>".$begin_m."<br>"; 
echo "<b>END: </b>".$end_m."<br>"; 
echo "<b>FILM ID: </b>".$film_id_m."<br>"; 

P.S: Sorry for my english.

Upvotes: 2

Views: 431

Answers (1)

raina77ow
raina77ow

Reputation: 106385

As film_id is an attribute of the currently processed element, you can retrieve its value with either...

$film_id_m = $cinema[0]->attributes()->film_id;

... or just...

$film_id_m = $cinema[0]['film_id'];

Upvotes: 2

Related Questions