2014-02-12 54 views
0

我不確定我是以正確的方式進行的,但我試圖從數組中回顯單個數據元素,但沒有成功,我只需要抓住10個變量的平均油耗從XML文件中的位置:https://www.fueleconomy.gov/ws/rest/ympg/shared/vehicles?make=honda&model=civicXML到關聯數組並回顯出特定值

我只需要製造商,型號,年份avgMpg這是youMpgVehicle等的孩子,所以我可以將它們放置在表內,在同一個像你可以重複出PHP中的SQL數據。

function download_page($path){ 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$path); 
curl_setopt($ch, CURLOPT_FAILONERROR,1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 15); 
//curl_setopt($ch, CURLOPT_SSLVERSION,3); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 

$retValue = curl_exec($ch);   
curl_close($ch); 
return $retValue; 
} 

$sXML = download_page('https://www.fueleconomy.gov/ws/rest/ympg/shared/vehicles?make=honda&model=civic'); 
$oXML = new SimpleXMLElement($sXML); 

$dom = new DomDocument(); 
$dom->loadXml($sXML); 
$dataElements = $dom->getElementsByTagName('vehicle'); 
$array = array(); 
foreach ($dataElements as $element) { 
$subarray = array(); 
foreach ($element->childNodes as $node) { 
    if (!$node instanceof DomElement) { 
     continue; 
    } 
    $key = $node->tagName; 
    $value = $node->textContent; 
    $subarray[$key] = $value; 
} 
$array[] = $subarray; 
// var_dump($array); // returns the array as expected 

var_dump($array[0]["barrels08"]); //how can I get this and other variables? 


} 

的結構是這樣的:(或者你可以點擊上面的超鏈接)

-<vehicles> 
-<vehicle> 
<atvType/> 
<barrels08>10.283832</barrels08> 
<barrelsA08>0.0</barrelsA08> 
<charge120>0.0</charge120> 
<charge240>0.0</charge240> 
<city08>28</city08> 
<city08U>28.0743</city08U> 
<cityA08>0</cityA08> 
<cityA08U>0.0</cityA08U> 
<cityCD>0.0</cityCD> 
<cityE>0.0</cityE> 
<cityUF>0.0</cityUF> 
<co2>279</co2> 
<co2A>-1</co2A> 
<co2TailpipeAGpm>0.0</co2TailpipeAGpm> 
<co2TailpipeGpm>279.0</co2TailpipeGpm> 
<comb08>32</comb08> 
<comb08U>31.9768</comb08U> 
<combA08>0</combA08> 
<combA08U>0.0</combA08U> 
<combE>0.0</combE> 
<combinedCD>0.0</combinedCD> 
<combinedUF>0.0</combinedUF> 
<cylinders>4</cylinders> 
<displ>1.8</displ> 
<drive>Front-Wheel Drive</drive> 
<engId>18</engId> 
<eng_dscr/> 
<evMotor/> 
<feScore>8</feScore> 
<fuelCost08>1550</fuelCost08> 
<fuelCostA08>0</fuelCostA08> 
<fuelType>Regular</fuelType> 
<fuelType1/> 
<fuelType2/> 
<ghgScore>8</ghgScore> 
<ghgScoreA>-1</ghgScoreA> 
<guzzler/> 
<highway08>39</highway08> 
<highway08U>38.5216</highway08U> 
<highwayA08>0</highwayA08> 
<highwayA08U>0.0</highwayA08U> 
<highwayCD>0.0</highwayCD> 
<highwayE>0.0</highwayE> 
<highwayUF>0.0</highwayUF> 
<hlv>0</hlv> 
<hpv>0</hpv> 
<id>33504</id> 
<lv2>12</lv2> 
<lv4>12</lv4> 
<make>Honda</make> 
<mfrCode>HNX</mfrCode> 
<model>Civic</model> 
<mpgData>Y</mpgData> 
<phevBlended>false</phevBlended> 
<pv2>83</pv2> 
<pv4>95</pv4> 
<rangeA/> 
<rangeCityA>0.0</rangeCityA> 
<rangeHwyA>0.0</rangeHwyA> 
<trans_dscr/> 
<trany>Automatic 5-spd</trany> 
<UCity>36.4794</UCity> 
<UCityA>0.0</UCityA> 
<UHighway>55.5375</UHighway> 
<UHighwayA>0.0</UHighwayA> 
<VClass>Compact Cars</VClass> 
<year>2013</year> 
<youSaveSpend>3000</youSaveSpend> 

- 33.612226599

+0

此代碼按預期工作。 'var_dump($ array [0] [「barrels08」]);'轉儲正確的數據。你有什麼問題? –

+0

是的,似乎工作,但如果你改變var_dump($ array [0] [「barrelsA08」]);這是下一個節點,它不,除非我犯了一個錯誤。將0改爲1,2,3等似乎也沒有什麼區別。 – user3270591

回答

0

如果您只想顯示數據,實際上並不需要將所有內容都放入數組中。 SimpleXML使處理XML數據變得非常簡單。如果我可以提出一個也許不太複雜的解決方案:

<?php 

function getFuelDataAsXml($make, $model) 
{ 
    // In most cases CURL is overkill, unless you need something more complex 
    $data = file_get_contents("https://www.fueleconomy.gov/ws/rest/ympg/shared/vehicles?make={$make}&model={$model}"); 

    // If we got some data, return it as XML, otherwise return null  
    return $data ? simplexml_load_string($data) : null; 
} 

// get the data for a specific make and model 
$data = getFuelDataAsXml('honda', 'civic'); 

// iterate over all vehicle-nodes 
foreach($data->vehicle as $vehicleData) 
{ 
    echo $vehicleData->barrels08 . '<br />'; 
    echo $vehicleData->yourMpgVehicle->avgMpg . '<br />'; 
    echo '<hr />'; 
} 
+0

謝謝,那看起來就像我在找的東西!非常感謝您的幫助! – user3270591

+0

@ user3270591很高興我能幫忙:) – Quasdunk

0

要從DOM使用XPath提取數據:

$url = "https://www.fueleconomy.gov/ws/rest/ympg/shared/vehicles?make=honda&model=civic"; 

$dom = new DomDocument(); 
$dom->load($url); 
$xpath = new DOMXpath($dom); 

foreach ($$xpath->evaluate('/*/vehicle') as $vehicle) { 
    var_dump(   
    array(
     $xpath->evaluate('string(fuelType)', $vehicle), 
     $xpath->evaluate('number(fuelCost08)', $vehicle), 
     $xpath->evaluate('number(barrels08)', $vehicle) 
    ) 
); 
} 

大多數的XPath表達式返回一個可以使用的foreach迭代節點列表。使用number()string()會將第一個節點的值或內容轉換爲浮點或字符串。如果列表爲空,您將獲得一個空值。