微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用php解析XML文件

我想解析以下xml并获取元数据的值:使用PHP进行描述

我知道如何获得头衔的价值

$item_title = $x -> item($i) -> getElementsByTagName('title') -> item(0) -> childNodes -> item(0) -> nodeValue;

但不能用这种方式获取元数据:描述

<item>
  <title>Jobs: Bullish Economy Confirmed?</title>
  <Metadata:title xmlns:Metadata="http://search.cnbc.com/RSS/2.0/modules/siteContentMetadata">Jobs: Bullish Economy Confirmed?  06 Jan 2012</Metadata:title>
  <description>discussing whether the better than expected jobs number point to an economic rebound, and where to invest in this market, with Michael Farr, Farr, Miller, &amp; Washington president.</description>
  <Metadata:description xmlns:Metadata="http://search.cnbc.com/RSS/2.0/modules/siteContentMetadata"><![CDATA[<div class="RSS_image" style="float:left;padding-right:10px;"><img border="0" vspace="0" hspace="0" width="93" src="http://thumbnails.cnbc.com/Vcps/Y2012/M01D06/3000066213/6ED1-KR-Jobs_sm.jpg"></div><div class="RSS_abstract" style="font:Arial 12px;width:100%;float:left;clear:both">discussing whether the better than expected jobs number point to an economic rebound, and where to invest in this market, with Michael Farr, Farr, Miller, &amp; Washington president.</div>]]></Metadata:description>
  <pubDate>Sat, 07 Jan 2012 00:24 GMT</pubDate>
  <guid isPermaLink="false">http://www.cnbc.com//id/15840232?video=3000066213&amp;play=1</guid>
  <link>http://www.cnbc.com//id/15840232?video=3000066213&amp;play=1</link>
</item>

原始xml文件链接

http://www.cnbc.com/id/19838222/device/RSS/RSS.xml

解决方法:

使用SimpleXML

$xmlstr='<item>
<title>Jobs: Bullish Economy Confirmed?</title>
<Metadata:title xmlns:Metadata="http://search.cnbc.com/RSS/2.0/modules/siteContentMetadata">Jobs: Bullish Economy Confirmed?  06 Jan 2012</Metadata:title>
<description>discussing whether the better than expected jobs number point to an economic rebound, and where to invest in this market, with Michael Farr, Farr, Miller, &amp; Washington president.</description>
<Metadata:description xmlns:Metadata="http://search.cnbc.com/RSS/2.0/modules/siteContentMetadata"><![CDATA[<div class="RSS_image" style="float:left;padding-right:10px;"><img border="0" vspace="0" hspace="0" width="93" src="http://thumbnails.cnbc.com/Vcps/Y2012/M01D06/3000066213/6ED1-KR-Jobs_sm.jpg"></div><div class="RSS_abstract" style="font:Arial 12px;width:100%;float:left;clear:both">discussing whether the better than expected jobs number point to an economic rebound, and where to invest in this market, with Michael Farr, Farr, Miller, &amp; Washington president.</div>]]></Metadata:description>
<pubDate>Sat, 07 Jan 2012 00:24 GMT</pubDate>
<guid isPermaLink="false">http://www.cnbc.com//id/15840232?video=3000066213&amp;play=1</guid>
<link>http://www.cnbc.com//id/15840232?video=3000066213&amp;play=1</link>
</item>';

$x= new SimpleXMLElement($xmlstr);
echo $x->title;
$nss = $x->getNameSpaces(true);
$Metadata = $x->children($nss['Metadata']); 
echo $Metadata->title, "\n";
echo $Metadata->description, "\n";

产量

Jobs: Bullish Economy Confirmed?Jobs: Bullish Economy Confirmed?  06 Jan 2012
<div class="RSS_image" style="float:left;padding-right:10px;"><img border="0" vspace="0" hspace="0" width="93" src="http://thumbnails.cnbc.com/Vcps/Y2012/M01D06/3000066213/6ED1-KR-Jobs_sm.jpg"></div><div class="RSS_abstract" style="font:Arial 12px;width:100%;float:left;clear:both">discussing whether the better than expected jobs number point to an economic rebound, and where to invest in this market, with Michael Farr, Farr, Miller, &amp; Washington president.</div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐