根据PHP中某个xml节点的属性获取该节点的内容


Get content of certain xml node based on its attribute in PHP

我有一个要从中提取内容的XML工作表。

这就是XML的样子:

<scores sport="soccer">
<script/>
   <category name="England: Premier League" id="1204" file_group="england">
      <matches date="Aug 24" formatted_date="24.08.2013">
         <match status="11:45" date="Aug 24" formatted_date="24.08.2013" time="11:45" static_id="1483470" fix_id="1395250" id="1556405">
            <localteam name="Fulham" goals="?" id="9175"/>
            <visitorteam name="Arsenal" goals="?" id="9002"/>
            <events/>
            <ht score=""/>
         </match>
      <match status="14:00" date="Aug 24" formatted_date="24.08.2013" time="14:00" static_id="1483469" fix_id="1395249" id="1556404">
            <localteam name="Everton" goals="?" id="9158"/>
            <visitorteam name="West Brom" goals="?" id="9426"/>
            <events/>
            <ht score=""/>
         </match>
      </matches>
   </category>
</scores>

我想要的是在PHP中以字符串的形式获取节点的属性。例如,我想获得1556404作为ID(并且只有那个马赫)的比赛的localteam名称和visitorsteam名称

有可能轻易解决这个问题吗?

Dan

使用XPath:

$xml = simplexml_load_file('http://url.com/xml');
$localTeamName = $xml
    ->xpath('/scores/category/matches/match[@id=1556404]/localteam/@name');