最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php 解析XML DOMXPath的例子
时间:2022-11-14 23:00:16 编辑:袖梨 来源:一聚教程网
xml文件结构如下
php代码解析:
$url="";//输出xml的URL $dom = new DOMDocument('1.0', 'utf-8'); $dom->load($url);
这样我们捕获里XML数据,然后一点点截取,这段XML有多个URL节点,需要利DOMPATH解析
$xpath = new DOMXPath($dom); $node = '//url'; $length = $xpath->query($node)->length;
然后根据每个url内的字节点去获取数据
for($i = 0; $i < $length; $i++) { $data = array(); $m_date = date('Y-m-d', strtotime($xpath->query($node.'['.($i+1).']/date')->item(0)->nodeValue)); $m_start = $xpath->query($node.'['.($i+1).']/start')->item(0)->nodeValue; $m_end = $xpath->query($node.'['.($i+1).']/end')->item(0)->nodeValue; $m_title = $xpath->query($node.'['.($i+1).']/title')->item(0)->nodeValue; $m_format = $xpath->query($node.'['.($i+1).']/format')->item(0)->nodeValue; $m_season = $xpath->query($node.'['.($i+1).']/season')->item(0)->nodeValue; $m_round = $xpath->query($node.'['.($i+1).']/round')->item(0)->nodeValue; /*$homeTeam = $xpath->query($node.'['.($i+1).']/homeTeam')->item(0)->nodeValue; if(empty($homeTeam)) continue; $guestTeam = $xpath->query($node.'['.($i+1).']/guestTeam')->item(0)->nodeValue; if(empty($guestTeam)) continue;*/ $homeTeam = $xpath->query($node.'['.($i+1).']/homeTeam')->item(0)->nodeValue; $guestTeam = $xpath->query($node.'['.($i+1).']/guestTeam')->item(0)->nodeValue; if(empty($guestTeam) || empty($homeTeam)) { if($m_title){ $temp_title=explode(" ", $m_title); list($homeTeam,$guestTeam)=explode("vs", $temp_title[1]); unset($temp_title); } } if(empty($homeTeam)) continue; if(empty($guestTeam)) continue; }
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21