我有一个带有特殊标签的xml rss我不知道如何阅读第二部分,在本例中是子类别
XML RSS:
<?xml version="1.0" encoding="UTF-8"?>
<channel>
<category text="Category">
<category text="Subcategory"/>
</category>
</channel>
</rss>
PHP
//this gives the category
$category = $feed->channel->category->attributes();
echo $category . '<br>';
//I tried this but only gives category and I need subcategory too
foreach ($feed->channel->category->attributes() as $item) {
echo $item . '<br>';
}
答案 0 :(得分:0)
那是因为你有两个元素,而不是两个属性;一个类别元素是另一个类别元素的嵌套元素
foreach ($feed->channel->category as $category) {
foreach($category->attributes() as $attribute) {
echo $attribute . '<br>';
}
foreach ($category->category as $subcategory) {
foreach($subcategory->attributes() as $attribute) {
echo $attribute . '<br>';
}
}
}
修改强>
如果您的代码段在语法上正确,和正确缩进
,则可能更容易理解<?xml version="1.0" encoding="UTF-8"?>
<rss>
<channel>
<category text="Category">
<category text="Subcategory"/>
</category>
</channel>
</rss>
因此category
元素可以包含“子”元素,也称为category