2017-11-18 112 views
0

我似乎無法弄清楚這一點。我正在嘗試從xml文件加載數據,具體取決於url參數的值。我有一個屬性設置在我的XML中,但似乎無法弄清楚如何去做這件事。我已經看了幾個獲取屬性的例子,所以我不確定我錯在哪裏,因爲我沒有遇到使用url參數來確定應該獲取哪些數據的例子。這是我到目前爲止。從URL參數獲取基於XML數據

我的xml文件的示例。在這個文件中會有更多的piercing的記錄。

<piercings> 
<piercing id="antieyebrow"> 
    <title>Anti-Eyebrow</title> 
    <names>Anti-Eyebrow, Teardrop</names> 
    <gauge>16</gauge> 
    <healing>6 - 8</healing> 
    <risk>Rejection/Migration, Scarring</risk> 
    <description>The anti-eyebrow piercing is located on the upper side of the cheek bone right below the orbital socket of the eye. This piercing is most commonly pierced using a 16 gauge curved barbell or custom bent jewelry. This piercing may also be referred to as a teardrop piercing.</description> 
    <aftercare>It is recommended with this piercing to clean twice a day using saline solution or antibacterial soap. Do not overclean. Irritation from overcleaning can result in migration of the piercing. 
</aftercare> 
    <avoid>Using rubbing alchohol as a cleaner. Changing the jewelry for atleast 3 weeks although recommended to wait until the piercing is fully healed. Pools/hot tubs especially those with chemical cleaners in them. Swimming holes, creeks, rivers, etc. due to bacterial exposure risk.</avoid> 
    <img>http://example.com/img/display/stock/antieyebrow_default.jpg</img> 
    <additionalimgs> 
    <additionalimg>http://example.com/img/thumb/stock/antieyebrow_1.jpg</additionalimg> 
    <additionalimg>http://example.com/img/thumb/stock/antieyebrow_2.jpg</additionalimg> 
    <additionalimg>http://example.com/img/thumb/stock/antieyebrow_3.jpg</additionalimg> 
    </additionalimgs> 
</piercing> 
</piercings> 

所以對於這一次我試圖用id="antieyebrow"屬性拉的所有數據的piercing。這是我試圖找回的東西。

if (file_exists($_SERVER['DOCUMENT_ROOT'].'/info/piercing/piercings.xml')) { 
    $piercingxml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/info/piercing/piercings.xml'); 
    $location = $_GET['location']; 
    $piercingid = $piercingxml['id'];     
} 
else { 
    $piercingxml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/info/piercing/default.xml'); 
    $location = "default"; 
} 

最後顯示的數據到頁面:

echo $piercingxml->$piercingid[$location]->title; 

任何幫助,因爲在那裏我出了錯,將不勝感激。

回答

1

這將有希望展示如何做你的事後。我正在使用XPath來查找包含你之後的id的正確元素。

$location = "antieyebrow"; // $_GET['location']; 
$details = $piercingxml->xpath("//piercing[@id='$location']")[0]; 
echo $details->title; 

我設置$location而不是使用一個參數 - 但這只是用於演示目的。

XPath的下一行說要查找稱爲piercing的任何(//位)元素。 []中的位是條件,這表示我想要查找具有id屬性(使用@ means屬性值)的元素,這是您之後的元素。 由於xpath返回匹配元素的列表,我剛剛採取了第一個(在此行末尾使用[0])。

在此之後$details包含你想要的元素,所以我可以使用$details->title來引用諸如<title>之類的東西。

+0

這是有道理的,我喜歡它多麼簡單is.Thanks。 –

1

你可以做的是在xpath表達式中使用$location參數,或者獲取所有穿透項並使用循環來檢查id屬性。 $item變量的類型爲SimpleXMLElement,並提供了一個xpath和一個attributes方法。

例如:

$piercingxml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/info/piercing/piercings.xml'); 
$location = $_GET['location']; 

// Use SimpleXMLElement xpath 
$expression = '/piercings/piercing[@id="' . $location . '"]'; 
foreach ($piercingxml->xpath($expression) as $item) { 
    echo $item->title; 
} 

// Use SimpleXMLElement and check the 'id' attribute 
foreach ($piercingxml->piercing as $item) { 
    if ((string)$item->attributes()->id === $location) { 
     echo $item->title; 
    } 
} 

都將給您:

反眉

+0

我喜歡你的解決方案,它的工作原理,但與我的網頁的結構是更實際的去與其他答案。無論如何,謝謝你。它給了我以後的另一個參考。 –