php简单的xml子节点变成了foreach

时间:2012-11-09 07:07:37

标签: php simplexml

我有一个XML文件。它看起来如下:

<Offers>
 <Offer>
  <OfferID>1</OfferID>
  <OfferName>Offer One</OfferName>
 </Offer>
 <Offer>
  <OfferID>2</OfferID>
  <OfferName>Offer Two</OfferName>
 </Offer>
</Offers>

我有一个如下所示的变量:

$latest_offers = simplexml_load_file("Offers.xml");

现在我的问题是我如何得到这样的出局:

foreach($offer as $item)
{
 echo <offerID>, <OfferName>...each and everything inside the offer nod
}

我怎样才能做到这一点。尝试了很多但没有成功。

1 个答案:

答案 0 :(得分:1)

试试这个。

<?php
$latest_offers = simplexml_load_file("Offers.xml");
foreach($latest_offers->Offer as $offer) {
   echo $offer->offerID . ', ' . $offer->OfferName . '<br>';
}
?>