simplexml php,PHP 使用 SimpleXML 遇到冒号「:」的解法

PHP 使用 SimpleXML 来解析 XML 很方便,解析 RSS 也是轻松愉快,不过要解析 WordPress 的 RSS 时,遇到 XML Tag 的名称有「:」,造成解析不到,要怎么解决呢?

PHP 使用 SimpleXML 遇到冒号「:」的解法

WordPress RSS 在内容的部分,有此标签:,而这个标签在 SimpleXML 解析是无法直接取用的。

下述有几种方式可以抓到:

方法1

下述的 http://purl.org/rss/1.0/modules/content/,取自 XML 最上面的定义,如下范例:

xmlns:content="http://purl.org/rss/1.0/modules/content/"

xmlns:wfw="http://wellformedweb.org/CommentAPI/"

xmlns:dc="http://purl.org/dc/elements/1.1/"

xmlns:atom="http://www.w3.org/2005/Atom"

xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"

xmlns:slash="http://purl.org/rss/1.0/modules/slash/"

xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

$rss = simplexml_load_file('http://feeds.feedburner.com/tsungblog');

foreach ($rss->channel->item as $i => $item) {

// echo $item->children('http://purl.org/dc/elements/1.1/')->encoded;

$ns_content = $item->children('http://purl.org/rss/1.0/modules/content/');

echo $ns_content->encoded;

}

?>

方法2

使用 PHP SimpleXMLElemenX children 的参数

public SimpleXMLElement SimpleXMLElement::children ([ string $ns [, bool $is_prefix = false ]] )

注:ns = namespace

$rss = simplexml_load_file('http://feeds.feedburner.com/tsungblog');

foreach ($rss->channel->item as $i => $item) {

$ns_content = $item->children('content', true);

echo $ns_content->encoded;

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值