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条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)