基于URL参数获取XML数据

时间:2017-11-18 03:33:08

标签: php xml

我似乎无法理解这一点。我试图从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>

因此,对于此问题,我尝试使用piercing属性提取id="antieyebrow"的所有数据。这是我试图检索的内容。

 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;

对于我出错的地方的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分: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包含您想要的元素后,我可以使用<title>引用$details->title之类的内容。

答案 1 :(得分:1)

您可以做的是在xpath表达式中使用$location参数或获取所有穿孔项并使用循环来检查id属性。 $item变量的类型为SimpleXMLElement,并提供xpathattributes方法。

例如:

$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;
    }
}

两者都会给你:

  

防眉