我似乎无法弄清楚这一点。我正在尝试从 xml 文件加载数据,具体取决于 url 参数的值。我在我的 xml 中设置了一个属性,但似乎无法弄清楚如何做到这一点。我已经查看了几个获取属性的示例,所以我不确定我错在哪里,因为我还没有遇到使用 url 参数来确定应该获取哪些数据的示例。这是我到目前为止所拥有的。
我的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;
任何关于我出错的帮助将不胜感激。
这将有望展示如何做你所追求的事情。 我正在使用 XPath 来查找具有您追求的 id 的正确元素。
$location = "antieyebrow"; // $_GET['location'];
$details = $piercingxml->xpath("//piercing[@id='$location']")[0];
echo $details->title;
我已经设置了$location
而不是使用参数 - 但这只是为了演示目的。
下一行是 XPath 说要查找任何称为 piercing
的(//
位(元素。[] 中的位是条件,这表示我想找到具有 id
属性的元素(使用 @ 表示属性值(,这是您之后的元素。由于xpath
返回匹配元素的列表,我刚刚选择了第一个元素(在此行末尾使用[0]
(。
在此之后,$details
包含您想要的元素,因此我可以使用 $details->title
引用诸如<title>
之类的内容。
您可以做的是在 xpath 表达式中使用 $location
参数或获取所有刺穿项并使用循环来检查 id 属性。$item
变量的类型为 SimpleXMLElement,并提供 xpath 和属性方法。
例如:
$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;
}
}
两者都会给你:
防眉毛