doubeng1278 2014-12-03 14: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 03: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部