帮助使用DOMDocument解析XML [英] Help parsing XML with DOMDocument
问题描述
我正在尝试解析youtube播放列表字段.
I am trying to parse a youtube playlist field.
URL为: http://gdata.youtube.com/feeds/api/playlists/664AA68C6E6BA19B?v = 2
我需要:标题,视频ID和默认缩略图.
I need: Title, Video ID, and Default thumbnail.
我可以轻松获得标题,但是在嵌套元素方面我有点迷茫
I can easily get the title but I'm a little lost when it comes to the nested elements
$data = new DOMDocument();
if($data->load("http://gdata.youtube.com/feeds/api/playlists/664AA68C6E6BA19B?v=2"))
{
foreach ($data->getElementsByTagName('entry') as $video)
{
$title = $video->getElementsByTagName('title')->item(0)->nodeValue;
$id = ??
$thumb = ??
}
}
这里是XML(我删除了与该示例无关的元素)
Here is the XML (I have stripped out the elements that are irrelevant for this example)
<entry gd:etag="W/"AkYGSXc9cSp7ImA9Wx9VGEk."">
<title>A GoPro Weekend On The Ice</title>
<media:group>
<media:thumbnail url="http://i.ytimg.com/vi/yk6wkfVNFQE/default.jpg" height="90" width="120" time="00:02:07" yt:name="default" />
<yt:videoid>yk6wkfVNFQE</yt:videoid>
</media:group>
</entry>
我需要缩略图默认设置中的视频ID"和网址"
I need the "videoid" and the "url" from thumbnail-default
谢谢!
推荐答案
类似于 getElementsByTagName()
可以使用 getElementsByTagNameNS()
方法访问命名空间元素(可由namespace:element-name
识别).
文档(如上链接)应该为您提供有关如何使用它的技术上的不足,足以说明它与以下类似(也请使用
The documenation (linked above) should give you the technical lowdown on how to use it, suffice to say it will be similar to the following (also using getAttribute()
).
$yt = 'http://gdata.youtube.com/schemas/2007';
$media = 'http://search.yahoo.com/mrss/';
// Inside your loop
$id = $video->getElementsByTagNameNS($yt, 'videoid')->item(0)->nodeValue;
$thumb = $video->getElementsByTagNameNS($media, 'thumbnail')->item(0)->getAttribute('url');
希望这将为您跳进访问XML文档中命名空间项的跳板.
Hopefully that should give you a spring-board to leap into accessing namespaced items within your XML documents.
这篇关于帮助使用DOMDocument解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!