一聚教程网:一个值得你收藏的教程网站

热门教程

php 解析XML DOMXPath的例子

时间:2022-11-14 23:00:16 编辑:袖梨 来源:一聚教程网

xml文件结构如下

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;

 

}

热门栏目