PHP 使用节点子节点解析 xml
PHP parsing xml with node children
RSS 提要 https://moxie.foxnews.com/feedburner/world.xml 包含如下项目:
<link></link>
<title></title>
<media:group>
<media:content url="*.jpg" medium="image" isDefault="true"/>
<media:content url="*.jpg" medium="image" width="60" height="60"/>
</media:group>
<media:thumbnail url="*.jpg" width="60" height="60"/>
我知道如何显示 <link>
和 <title>
的内容,但是谁能帮我显示 <media:group><media:content>
和 <media:thumbnail>
中的媒体 URL?
谢谢
我已经清理了格式错误的 XML 所以它会解析并提供一种方法来查看错误是什么。
$xml = '<media:group>
<link></link>
<title></title>
<media:content url="*.jpg" medium="image" isDefault="true"></media:content>
<media:content url="*.jpg" medium="image" width="60" height="60"></media:content>
</media:group>';
libxml_use_internal_errors(true);
$doc = simplexml_load_string($xml);
if ($doc === false) {
$errors = libxml_get_errors();
print_r($errors);
} else {
print_r($doc);
}
结果
SimpleXMLElement Object
(
[link] => SimpleXMLElement Object
(
)
[title] => SimpleXMLElement Object
(
)
[media:content] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[url] => *.jpg
[medium] => image
[isDefault] => true
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[url] => *.jpg
[medium] => image
[width] => 60
[height] => 60
)
)
)
)
或直接来自 URL
libxml_use_internal_errors(true);
$doc = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
if ($doc === false) {
$errors = libxml_get_errors();
print_r($errors);
} else {
print_r($doc);
}
// 结果
简单XML元素对象
(
[@attributes] => 数组
(
[版本] => 2.0
)
[channel] => SimpleXMLElement Object
(
[title] => FOX News : World
[description] => SimpleXMLElement Object
(
)
[image] => SimpleXMLElement Object
(
[url] => https://global.fncstatic.com/static/orion/styles/img/fox-news/logos/fox-news-desktop.png
[title] => FOX News : World
[link] => https://www.foxnews.com
)
[link] => https://www.foxnews.com
[item] => Array
(
[0] => SimpleXMLElement Object
(
[guid] => https://www.foxnews.com/world/us-taiwan-launch-trade-talks-biden-excludes-island-indo-pacific-group
[link] => https://www.foxnews.com/world/us-taiwan-launch-trade-talks-biden-excludes-island-indo-pacific-group
[category] => Array
(
[0] => c8e27233-08e3-5b74-9eff-da5e9fe271ff
[1] => fox-news/world/world-regions/asia
[2] => fox-news/world/world-regions/china
[3] => fox-news/person/joe-biden
[4] => fox-news/politics/foreign-policy
[5] => fnc
[6] => fox-news/world
[7] => article
[8] => Reuters
)
[title] => US, Taiwan to launch trade talks after Biden excludes island from Indo-Pacific group
[description] => SimpleXMLElement Object
(
)
[pubDate] => Wed, 01 Jun 2022 15:25:51 GMT
感谢您的澄清。
$xml = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
foreach ($xml->channel->item as $item) {
foreach ($item->children('media', true) as $mg) {
$media_group = $mg->content->attributes()['url'];
print_r($media_group);
}
}
// 结果
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/lech-walesa.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/taiwan-us-flag.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/05/Putin.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/05/GettyImages-1395351622-1-e1651678769232.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/04/Dmitry-Peskov.png
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/Ukraine-President-Volodymyr-Zelenskyy-Russia-War.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/04/Israeli-Soldier.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2021/11/chinaforeignministryspokesman.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/02/zelenskyy-biden-split-2-25-22.jpg
)
更新
$rss = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
foreach ($rss->channel->item as $item) {
echo '<p><a href="' . $item->link . '">' . $item->title . "</a></p>";
echo "<p>" . $item->description . "</p>";
foreach ($item->children('media', true) as $mg) {
foreach ($mg->children('media', true) as $c) {
echo '<img src="' . $c->attributes()['url'] . '"><br>';
}
}
}
// 输出(注意第二张图片是缩略图)
<p>
<a href="https://www.foxnews.com/world/russia-global-food-crisis-49-million-famine-starvation-expert">Russia-induced global food crisis pushes 49M to 'brink' of famine, starvation, expert warns</a>
</p>
<p>Russia's war in Ukraine has sparked a global food crisis with 49 million facing famine. Now, experts are worried the world faces an increased security threat.</p>
<img src="https://static.foxnews.com/foxnews.com/content/uploads/2021/09/World-Food-Program-Kabul-Afghanistan..jpg"><br>
<img src="https://a57.foxnews.com/static.foxnews.com/foxnews.com/content/uploads/2021/09/60/60/World-Food-Program-Kabul-Afghanistan..jpg?ve=1&tl=1">
RSS 提要 https://moxie.foxnews.com/feedburner/world.xml 包含如下项目:
<link></link>
<title></title>
<media:group>
<media:content url="*.jpg" medium="image" isDefault="true"/>
<media:content url="*.jpg" medium="image" width="60" height="60"/>
</media:group>
<media:thumbnail url="*.jpg" width="60" height="60"/>
我知道如何显示 <link>
和 <title>
的内容,但是谁能帮我显示 <media:group><media:content>
和 <media:thumbnail>
中的媒体 URL?
谢谢
我已经清理了格式错误的 XML 所以它会解析并提供一种方法来查看错误是什么。
$xml = '<media:group>
<link></link>
<title></title>
<media:content url="*.jpg" medium="image" isDefault="true"></media:content>
<media:content url="*.jpg" medium="image" width="60" height="60"></media:content>
</media:group>';
libxml_use_internal_errors(true);
$doc = simplexml_load_string($xml);
if ($doc === false) {
$errors = libxml_get_errors();
print_r($errors);
} else {
print_r($doc);
}
结果
SimpleXMLElement Object
(
[link] => SimpleXMLElement Object
(
)
[title] => SimpleXMLElement Object
(
)
[media:content] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[url] => *.jpg
[medium] => image
[isDefault] => true
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[url] => *.jpg
[medium] => image
[width] => 60
[height] => 60
)
)
)
)
或直接来自 URL
libxml_use_internal_errors(true);
$doc = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
if ($doc === false) {
$errors = libxml_get_errors();
print_r($errors);
} else {
print_r($doc);
}
// 结果
简单XML元素对象 ( [@attributes] => 数组 ( [版本] => 2.0 )
[channel] => SimpleXMLElement Object
(
[title] => FOX News : World
[description] => SimpleXMLElement Object
(
)
[image] => SimpleXMLElement Object
(
[url] => https://global.fncstatic.com/static/orion/styles/img/fox-news/logos/fox-news-desktop.png
[title] => FOX News : World
[link] => https://www.foxnews.com
)
[link] => https://www.foxnews.com
[item] => Array
(
[0] => SimpleXMLElement Object
(
[guid] => https://www.foxnews.com/world/us-taiwan-launch-trade-talks-biden-excludes-island-indo-pacific-group
[link] => https://www.foxnews.com/world/us-taiwan-launch-trade-talks-biden-excludes-island-indo-pacific-group
[category] => Array
(
[0] => c8e27233-08e3-5b74-9eff-da5e9fe271ff
[1] => fox-news/world/world-regions/asia
[2] => fox-news/world/world-regions/china
[3] => fox-news/person/joe-biden
[4] => fox-news/politics/foreign-policy
[5] => fnc
[6] => fox-news/world
[7] => article
[8] => Reuters
)
[title] => US, Taiwan to launch trade talks after Biden excludes island from Indo-Pacific group
[description] => SimpleXMLElement Object
(
)
[pubDate] => Wed, 01 Jun 2022 15:25:51 GMT
感谢您的澄清。
$xml = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
foreach ($xml->channel->item as $item) {
foreach ($item->children('media', true) as $mg) {
$media_group = $mg->content->attributes()['url'];
print_r($media_group);
}
}
// 结果
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/lech-walesa.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/taiwan-us-flag.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/05/Putin.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/05/GettyImages-1395351622-1-e1651678769232.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/04/Dmitry-Peskov.png
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/Ukraine-President-Volodymyr-Zelenskyy-Russia-War.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/04/Israeli-Soldier.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2021/11/chinaforeignministryspokesman.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/02/zelenskyy-biden-split-2-25-22.jpg
)
更新
$rss = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
foreach ($rss->channel->item as $item) {
echo '<p><a href="' . $item->link . '">' . $item->title . "</a></p>";
echo "<p>" . $item->description . "</p>";
foreach ($item->children('media', true) as $mg) {
foreach ($mg->children('media', true) as $c) {
echo '<img src="' . $c->attributes()['url'] . '"><br>';
}
}
}
// 输出(注意第二张图片是缩略图)
<p>
<a href="https://www.foxnews.com/world/russia-global-food-crisis-49-million-famine-starvation-expert">Russia-induced global food crisis pushes 49M to 'brink' of famine, starvation, expert warns</a>
</p>
<p>Russia's war in Ukraine has sparked a global food crisis with 49 million facing famine. Now, experts are worried the world faces an increased security threat.</p>
<img src="https://static.foxnews.com/foxnews.com/content/uploads/2021/09/World-Food-Program-Kabul-Afghanistan..jpg"><br>
<img src="https://a57.foxnews.com/static.foxnews.com/foxnews.com/content/uploads/2021/09/60/60/World-Food-Program-Kabul-Afghanistan..jpg?ve=1&tl=1">