PHP 检索 XML 标记名
我实际上对这些 XML 东西很陌生。我确实了解有关 DOMDocument、DOMNOdeList 等的内容,使用 http://www.php .net/manual/en/intro.dom.php
所以这就是问题所在..
http://jobhits.co.uk/services/rss?k=job
上面的提要返回一个 XML 文档。我可以使用这些代码成功检索标签名称,如标题、描述和链接,
$doc->load('http://jobhits.co.uk/services/rss?k=job');
$items = $doc->getElementsByTagName("item");
foreach($items as $item){
$titles[] = $item->getElementsByTagName("title");
}
问题是该文档中存在某个“类似于标签名称”,
<a10:updated></a10:updated>
我尝试使用
$update[] = $item->getElementsByTagName("a10:updated");
..
这 是一个失败示例 xml http://piratelufi.com/ark/gettagname.xml 或者您可以使用上面的 load 方法中的字符串:)
顺便说一句,我不能尽可能多地使用 simpleXML 和预定义的类 谢谢:D
i am actually new with these XML stuff. I do understand things about the DOMDocument,DOMNOdeList and etc using http://www.php.net/manual/en/intro.dom.php
so here is the problem..
http://jobhits.co.uk/services/rss?k=job
the feed above returns an XML document. i can successfully retrieve tag names like title,description and link using these codes
$doc->load('http://jobhits.co.uk/services/rss?k=job');
$items = $doc->getElementsByTagName("item");
foreach($items as $item){
$titles[] = $item->getElementsByTagName("title");
}
the problem is there is a certain 'tagname-like' in that document
<a10:updated></a10:updated>
i tried getting that using
$update[] = $item->getElementsByTagName("a10:updated");
..which is a failure
here is a sample xml http://piratelufi.com/ark/gettagname.xml or you can use the string inside load method above :)
btw i can't use simpleXML and predefined classes as much as possible
thanks :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找
getElementsByTagNameNS
,You are looking for
getElementsByTagNameNS
,a10
jsut 表示更新的元素来自不同的命名空间。在此上下文中,冒号:
是一个特殊字符。在示例 xml 的开头(后一个 url),可以找到此命名空间的定义:
。所以你需要
getElementsByTagNameNS
。我认为以下内容:getElementsByTagNameNS("http://www.w3.org/2005/Atom","updated")
可能会有所帮助。The
a10
jsut denotes, that the element updated is from a different namespace. The colon:
is a special character in this context.In the beginning of your sample xml (the latter url) one finds the definition of this namespace:
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
.So You need
getElementsByTagNameNS
. I presume something along the lines of:getElementsByTagNameNS("http://www.w3.org/2005/Atom","updated")
might help.