1
我使用PHP simplexml解析了以下形式的xml结构。在PHP中使用SimpleXML解析XML
<books>
<book>
<title>XYZ</title>
<author> someone </author>
<images>
<image type="poster" url="<url>" size="cover" id="12345"/>
<image type="poster" url="<url>" size="thumb" id="12345"/>
</images>
</book>
<book>
<title>PQR</title>
<author> someoneelse </author>
<images>
<image type="poster" url="<url>" size="cover" id="67890"/>
<image type="poster" url="<url>" size="thumb" id="67890"/>
</images>
</book>
</books>
假设我想打印第一本书的标题。我能够做到这一点使用
但是,当我尝试打印这本书的所有图像网址它不起作用。代码即时通讯使用是:
$books = $xml->books;
$book = $books->book[0]; //Get the first book
$images=$book->images;
foreach($images as $image) //This does not work
{
print $image->url;
}
任何方式来解决这个问题?
谢谢
感谢您的回复昵称。但我似乎在这里有一个奇怪的问题。打印$ images-> image [0] - > attributes() - > url正确打印第一张图片的url,但下面的循环打印所有图片的url不起作用......这里可能是错误的? foreach($ image as $ image){print $ image-> attributes() - > url; } – John 2009-12-22 02:56:59
你还需要做foreach($ images-> image as $ image) – jmans 2009-12-22 03:14:17