2015-10-09 111 views
2

我正在努力阅读使用PHP的XML文件。从XML获取数据

我想使用的XML是在这里: http://www.gdacs.org/xml/rss.xml

现在,我很感兴趣的数据是“项目”节点。

我创建了下面的函数,它获取的数据:

$rawData = simplexml_load_string($response_xml_data); 

foreach($rawData->channel->item as $value) { 
    $title = $value->title; 

.... 能正常工作。

与节点“GDCS:XXXX”稍微更多的问题,但我用下面的代码,这也可以工作:

$subject = $value->children('dc', true)->subject; 

现在我的问题是与“资源”节点,

基本上它的精简版是这样的:

<channel> 
    <item> 
     <gdacs:resources> 
     <gdacs:resource id="xx" version="0" source="xx" url="xx" type="xx"> 
      <gdacs:title>xxx</gdacs:title> 
     </gdacs:resource> 
     <gdacs:resource id="xx" version="0" source="xx" url="xx" type="xx"> 
      <gdacs:title>xxx</gdacs:title> 
     </gdacs:resource> 
     <gdacs:resource id="xx" version="0" source="xx" url="xx" type="xx"> 
      <gdacs:title>xxx</gdacs:title> 
     </gdacs:resource> 
     </gdacs:resources> 
    </item> 
</channel> 

在这种情况下,我将如何获得资源?我始终能够获得第一个资源,只有它的标题。我想要做的是获得所有资源项目,它们具有特定值的“类型”并获取其URL。

回答

1

考虑使用带有方括号的xpath的节点出现[]来将url与相应的titles对齐。对@Daniel Batkilin的答案进行更多涉及的修改,可以将这两个数据片段并入关联多维数组中,要求嵌套for循环。

$xml = simplexml_load_file('http://www.gdacs.org/xml/rss.xml'); 
$xml->registerXPathNamespace('gdacs', 'http://www.gdacs.org'); 

$items = $xml->xpath("//channel/item"); 

$i = 1; 
$out = array(); 
foreach($items as $x) {  

    $titles = $xml->xpath("//channel/item[".$i."]/gdacs:resources/gdacs:resource[@type='image']/gdacs:title"); 
    $urls = $xml->xpath("//channel/item[".$i."]/gdacs:resources/gdacs:resource[@type='image']/@url"); 

    for($j=0; $j<count($urls); $j++) { 

     $out[$j.$i]['title'] = (string)$titles[$j]; 
     $out[$j.$i]['url'] = (string)$urls[$j];      

    } 

    $i++; 
} 

$out = array_values($out); 
var_dump($out); 

ARRAY DUMP

array(40) { 
    [0]=> 
    array(2) { 
    ["title"]=> 
    string(21) "Storm surge animation" 
    ["url"]=> 
    string(92) "http://webcritech.jrc.ec.europa.eu/ModellingCyclone/cyclonesurgeVM/1000226/final/outres1.gif" 
    } 
    [1]=> 
    array(2) { 
    ["title"]=> 
    string(26) "Storm surge maximum height" 
    ["url"]=> 
    string(101) "http://webcritech.jrc.ec.europa.eu/ModellingCyclone/cyclonesurgeVM/1000226/final/P1_MAXHEIGHT_END.jpg" 
    } 
    [2]=> 
    array(2) { 
    ["title"]=> 
    string(12) "Overview map" 
    ["url"]=> 
    string(64) "http://dma.gdacs.org/saved/gdacs/tc/1000226/clouds_1000226_2.png" 
    } 
    [3]=> 
    array(2) { 
    ["title"]=> 
    string(41) "Map of rainfall accummulation in past 24h" 
    ["url"]=> 
    string(70) "http://dma.gdacs.org/saved/gdacs/tc/1000226/current_rain_1000226_2.png" 
    } 
    [4]=> 
    array(2) { 
    ["title"]=> 
    string(23) "Map of extreme rainfall" 
    ["url"]=> 
    string(62) "http://dma.gdacs.org/saved/gdacs/tc/1000226/rain_1000226_2.png" 
    } 
    [5]=> 
    array(2) { 
    ["title"]=> 
    string(34) "Map of extreme rainfall (original)" 
    ["url"]=> 
    string(97) "http://www.ssd.noaa.gov/PS/TROP/DATA/ETRAP/2015/NorthIndian/THREE/2015THREE.pmqpf.10100000.00.GIF" 
    } 

... 
2

通过XML运行常规路径,从我的经验来看,速度缓慢而且令人难以忍受。

看一看到的XPath - >这是一个方法来提取,通过选择从XML数据(类似于CSS选择器)

http://php.net/manual/en/simplexmlelement.xpath.php

您可以通过类似于CSS的属性选择元素

<?php 
$xmlStr = file_get_contents('some_xml.xml'); 
$xml = new SimpleXMLElement($xmlStr); 

$items = $xml->xpath("//channel/item"); 

$urls_by_item = array(); 
foreach($items as $x) { 
    $urls_by_item [] = $x->xpath("//gdacs:resources/gdacs:resource[@type='image']/@url"); 
} 
+0

这个工作,但问题是,我得到的所有网址,所有的项目,我需要到特定的URL分配给特定项目,如果你看看在整个文件中,XML包含许多“项目”节点,我需要为每个“项目”获取类型为“image”的资源的URL。您建议的解决方案会在整个XML文件中生成所有xx类型资源的数组,而不管它们属于哪个项目 – Jachym

+0

为每个项目单独创建一个SimpleXMLElement,然后在循环中为每个项目运行xpath。您可以使用2个xpath语句 - >第一个获取所有项目,下一个使用上面的代码循环遍历结果 – DannyZB