php - XML 解析属性 PHP $string = value ;

标签 php parsing

我有 xml。 现在是电影放映时间,我尝试解析。

<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>

我解析 XML 并回显内容: 字符串“film_id_m”必须是标签中的 7043。

$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:对不起我的英语。

最佳答案

由于 film_id 是当前处理元素的属性,因此您可以使用以下任一方式检索其值...

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

...或者只是...

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

关于php - XML 解析属性 PHP $string = value ;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13473138/

相关文章:

php - 我无法访问 XAMPP phpMyAdmin;它说 : Error MySQL said: Documentation Cannot connect: invalid settings

在没有剪切的情况下在 Prolog 中解析?

python - 如何在python nltk中使用malt解析器

javascript - 带有预定义函数和变量的 PEG.js 扩展

php - 每次 PHPUnit 测试后换行

php - Eloquent 中的 If request is not null 语句

php - 如何创建和使用虚假目录结构?

javascript - 播放另一个视频时停止视频

c - 在C中从HTML <a></a>标签中解析URL的信息

python - 我怎样才能使这个语法明确?