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 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色