使用simplexml_load_string

时间:2018-05-08 16:54:11

标签: php xml simplexml

我正在尝试从XML文档中检索数据,如下所示:

<scores sport="soccer" updated="08.05.2018 09:39:42">
<category name="Argentina: Superliga" gid="1081" id="1081" file_group="argentina" iscup="False">
<matches date="May 08" formatted_date="08.05.2018">
<match status="FT" timer="" date="May 08" formatted_date="08.05.2018" time="00:15" commentary_available="argentina" venue="Estadio Brigadier General Estanislao López" v="60" static_id="2288469" fix_id="2172208" id="2442089">
<localteam name="Colon Santa FE" goals="0" id="5947"/>
<visitorteam name="River Plate" goals="0" id="6042"/>
<events>
<event type="yellowcard" minute="39" extra_min="" team="visitorteam" player="C. Mayada" result="" playerId="91623" assist="" assistid="" eventid="24420891"/>
<event type="yellowcard" minute="40" extra_min="" team="localteam" player="M. Fritzler" result="" playerId="14256" assist="" assistid="" eventid="24420892"/>
<event type="subst" minute="46" extra_min="" team="visitorteam" player="J. Quintero" result="" playerId="74510" assist="G. Martinez" assistid="51346" eventid="24420893"/>
<event type="subst" minute="63" extra_min="" team="visitorteam" player="L. Pratto" result="" playerId="17657" assist="R. Mora" assistid="55877" eventid="24420894"/>
<event type="subst" minute="63" extra_min="" team="localteam" player="N. Leguizamon" result="" playerId="443149" assist="C. Bernardi" assistid="186181" eventid="24420895"/>
<event type="subst" minute="70" extra_min="" team="localteam" player="P. Ledesma" result="" playerId="13875" assist="A. Bastia" assistid="15082" eventid="24420896"/>
<event type="subst" minute="73" extra_min="" team="visitorteam" player="R. Borre" result="" playerId="320836" assist="I. Scocco" assistid="15172" eventid="24420897"/>
<event type="subst" minute="82" extra_min="" team="localteam" player="L. Heredia" result="" playerId="337735" assist="A. Ruiz" assistid="189511" eventid="24420898"/>
<event type="yellowcard" minute="90" extra_min="1" team="visitorteam" player="R. Borre" result="" playerId="320836" assist="" assistid="" eventid="24420899"/>
</events>
<ht score="[0-0]"/>
<ft score="[0-0]"/>
</match>
</matches>
</category>
<category name="Argentina: Primera B Nacional - Promotion - Play Offs" gid="3899" id="1078" file_group="argentina" iscup="False">
<matches date="May 08" formatted_date="08.05.2018">
<match status="23:05" timer="" date="May 08" formatted_date="08.05.2018" time="23:05" commentary_available="" venue="" v="0" static_id="2369960" fix_id="2253925" id="2443571">
<localteam name="Almagro" goals="?" id="5900"/>
<visitorteam name="Agropecuario" goals="?" id="21385"/>
<events/>
<ht score=""/>
</match>
</matches>
</category>
</scores>

到目前为止我尝试过:

$xml=simplexml_load_string($content);
//print_r($xml);
foreach ($xml->scores->category->matches->match as $value) {
    echo (string)$value;
}

我收到的消息:

Notice: Trying to get property of non-object in C:\xampp\htdocs\proj\inc\get.php on line 23

Notice: Trying to get property of non-object in C:\xampp\htdocs\proj\inc\get.php on line 23

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\proj\inc\get.php on line 23

具体来说,我正在尝试提取日期,团队名称,目标等属性中的内容。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

使用SimpleXML加载XML文件时,根元素是任何访问的起点。因此,您不需要scores,因为这是$xml

foreach ($xml->category->matches->match as $value) {
    echo (string)$value['status'];
}

这会输出状态元素(FT),以显示它实际上是<match>元素。

更新:正如Syscall所说,这个XML的结构需要像......

foreach ($xml->category as $value1) {
    foreach ( $value1->matches->match as $value)    {
        echo (string)$value['status'];
    }
}

获取所有<match>元素。