simplexml_load_string foreach不显示任何内容

时间:2013-03-29 22:37:42

标签: php curl simplex

我的PHP代码如下所示,它显示为空白我想显示所有带有超链接的记录,如

<a href="test.php?id=$Id">$Name</a>

其中$ Id,$ Name来自XML Feed

  $path = 'www.abc.com/test.xml';
  $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);
  $returned = curl_exec($ch);
  curl_close($ch);
  // $xml === False on failure
  $xml = simplexml_load_string($returned);

foreach( $xml->Name as $Name){
   print (string)$Name;
}

MY XML如下所示

<ABCXml version="1.1.0">
  <Manufacturers>
   <Item>
    <Id>219</Id>
    <Name>Matrix Corp</Name>
   </Item>
   <Item>
    <Id>2040</Id>
    <Name>Microcomputer</Name>
   </Item>
 </Manufacturers>
</ABCXml>

1 个答案:

答案 0 :(得分:3)

您的xml对象以制造商开头,其中包含多个包含名称和ID的项目。你需要走出去。

foreach($xml->Manufacturers->Item as $item) {
    printf("<a href='test.php?id=%s>%s</a>", $item->Id, $item->Name);
}
相关问题