我有一个xml Feed,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<books version="1.0">
<book>
<NR><![CDATA[1]]></NR>
<title><![CDATA[somebook]]></title>
<picture><![CDATA[http://www.website.com/images/somebook_pic.jpg]]></picture>
</book>
</books>
一个无效的php循环:
<?php
$file = "books.php";
$html = "";
$url = "http://www.website.com/feed/feed.xml";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$number = $xml->book[$i]->NR;
$img = $xml->book[$i]->picture;
$title = $xml->book[$i]->title;
$html .= "<img src=\"$img\">$title - $number</a>";
}
file_put_contents($file, $html);
?>
当XML Feed没有时,它正在工作![CDATA [,不幸的是他们改变它(我不知道为什么),现在它没有用。
XML Feed更新:
<?xml version="1.0" encoding="UTF-8"?>
<books version="1.4">
<category name="science-fiction">
<book>
<NR><![CDATA[1]]></NR>
<title><![CDATA[somebook]]></title>
<picture><![CDATA[http://www.website.com/images/somebook_pic.jpg]]></picture>
</book>
<book>
<NR><![CDATA[2]]></NR>
<title><![CDATA[somebook2]]></title>
<picture><![CDATA[http://www.website.com/images/somebook2_pic.jpg]]></picture>
</book>
</category>
</books>
答案 0 :(得分:0)
你没有迭代<category>
元素;这应该有效:
$books = simplexml_load_string($xml);
$html = '';
foreach ($books->category->book as $book) {
$html .= sprintf('<img src="%s">%s - %s</a>',
htmlspecialchars($book->picture, ENT_QUOTES, 'UTF-8'),
htmlspecialchars($book->title, ENT_QUOTES, 'UTF-8'),
htmlspecialchars($book->NR, ENT_QUOTES, 'UTF-8')
);
}
echo $html, PHP_EOL;