How to parse XML file using php(如何使用 php 解析 XML 文件)
本文介绍了如何使用 php 解析 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想解析下面的xml并获取metadata:description using php的值
I want to parse the following xml and get the value of metadata:description using php
我知道如何获取title的值
I know how to get the value of title
$item_title = $x -> item($i) -> getElementsByTagName('title') -> item(0) -> childNodes -> item(0) -> nodeValue;
但不能用这种方式获取元数据:描述
but can not use this way to get metadata:description
<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, & 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="aHR0cDovL3RodW1ibmFpbHMuY25iYy5jb20vVkNQUy9ZMjAxMi9NMDFEMDYvMzAwMDA2NjIxMy82RUQxLUtSLUpvYnNfc20uanBn"></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, & 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&play=1</guid>
<link>http://www.cnbc.com//id/15840232?video=3000066213&play=1</link>
</item>
<小时>
原始xml文件链接
Original xml file link
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, & 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="aHR0cDovL3RodW1ibmFpbHMuY25iYy5jb20vVkNQUy9ZMjAxMi9NMDFEMDYvMzAwMDA2NjIxMy82RUQxLUtSLUpvYnNfc20uanBn"></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, & 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&play=1</guid>
<link>http://www.cnbc.com//id/15840232?video=3000066213&play=1</link>
</item>';
$x= new SimpleXMLElement($xmlstr);
echo $x->title;
$nss = $x->getNameSpaces(true);
$metadata = $x->children($nss['metadata']);
echo $metadata->title, "
";
echo $metadata->description, "
";
输出
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="aHR0cDovL3RodW1ibmFpbHMuY25iYy5jb20vVkNQUy9ZMjAxMi9NMDFEMDYvMzAwMDA2NjIxMy82RUQxLUtSLUpvYnNfc20uanBn"></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, & Washington president.</div>
这篇关于如何使用 php 解析 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何使用 php 解析 XML 文件


猜你喜欢
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- Laravel 仓库 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01