doubeng1278 2014-12-03 22:10
浏览 19
已采纳

使用PHP解析XML Feed以查找是否存在特定数据

I need to check if Team1 exists in the XML feed. If Team1 appears twice in the XML feed, I need to output only the first instance. So far I can only output both instances. How do I accomplish this?

$str = <<<'XML'
<DATAS>
    <DATA>
        <VISITOR>Team6</VISITOR>
        <HOME>Team7</HOME>
    </DATA>
    <DATA>
        <VISITOR>Team1</VISITOR>
        <HOME>Team2</HOME>
    </DATA>
    <DATA>
        <VISITOR>Team3</VISITOR>
        <HOME>Team1</HOME>
    </DATA>
    <DATA>
        <VISITOR>Team4</VISITOR>
        <HOME>Team5</HOME>
    </DATA>
</DATAS>    
XML;


$data = new SimpleXMLElement($str);

$found = false;

foreach ($data->DATA as $item) {
    $teamh = $item->HOME;
    $teamv = $item->VISITOR;


    if ($teamh == 'Team1' || $teamv == 'Team1') { 
        $found = true;

    echo 'Home Team: ' . $data->DATA->HOME . "
";
    echo 'Away Team: ' . $data->DATA->VISITOR . "
";
   }

}
  • 写回答

2条回答 默认 最新

  • drb88830 2014-12-04 11:15
    关注

    Use XPath. Selecting nodes on a DOM tree is easy with it. (SimpleXML uses a DOM in the background). Here are to methods for it SimpleXMLElement::xpath() and DOMXpath::evaluate().

    Select all event data:
    /DATAS/DATA

    Only if VISITOR or HOME is "Team1":
    /DATAS/DATA[VISITOR='Team1' or HOME='Team1']

    Count them:
    count(/DATAS/DATA[VISITOR='Team1' or HOME='Team1'])

    Limit to the first found node:
    /DATAS/DATA[VISITOR='Team1' or HOME='Team1'][1]

    SimpleXMLElement::xpath() always returns an array of SimpleXMLElement objects. So it can not directly execute the count(...) Xpath expression.

    You can of course use the PHP function count() on the return value.

    count($datas->xpath("/DATAS/DATA[VISITOR='Team1' or HOME='Team1']"));
    

    If you only want to output the first event data for a team if it exists you will not need the count. Limiting the result in XPath will return a list/array with a single element or an empty list.

    SimpleXML Example

    $datas = simplexml_load_string($str);
    
    $eventCount = count($datas->xpath("/DATAS/DATA[VISITOR='Team1' or HOME='Team1']"));
    echo "Team1 is included $eventCount time(s) in the feed.
    ";
    
    $events = $datas->xpath("/DATAS/DATA[VISITOR='Team1' or HOME='Team1'][1]");
    foreach ($events as $event) {
      echo "Visitor: {$event->VISITOR}, Home: {$event->HOME}
    "; 
    }
    

    DOM Example

    $dom = new DOMDocument();
    $dom->loadXml($str);
    $xpath = new DOMXPath($dom);
    
    $eventCount = $xpath->evaluate("count(/DATAS/DATA[VISITOR='Team1' or HOME='Team1'])");
    echo "Team1 is included $eventCount time(s) in the feed.
    ";
    
    $events = $xpath->evaluate("/DATAS/DATA[VISITOR='Team1' or HOME='Team1'][1]");
    foreach ($events as $event) {
      $visitor = $xpath->evaluate('string(VISITOR)', $event);
      $home = $xpath->evaluate('string(HOME)', $event);
      echo "Visitor: $visitor, Home: $home
    "; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了