php读取xml列表程序
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-20 16:07:39 浏览: 评论:0
php读了xml很方便的,我们下面用dom与php自带的xml_parser来实例吧,我们先看看wk.xml 文件,其实这里是blogbus的rss文件,代码如下:
- <?xml version="1.0" encoding="gb2312"?>
- <rss version="2.0">
- <channel>
- <title>mikeowen</title>
- <link>http://mikeowen.blogbus.com</link>
- <description><![CDATA[Design]]></description>
- <generator> by blogbus.com </generator>
- <lastBuildDate>Tue, 30 Jan 2007 13:46:52 +0800</lastBuildDate>
- <image>
- <url>http://public.blogbus.com/images/head.gif</url>
- <title>mikeowen</title>
- <link>http://mikeowen.blogbus.com</link>
- </image>
- <item>
- <title>vanke</title>
- <description>tff</description>
- <link>http://mikeowen.blogbus.com/logs/28560579.html</link>
- <author>mikeowen</author>
- <pubDate>Fri, 05 Sep 2008 12:41:22 +0800</pubDate>
- </item>
- <item>
- <title>something3</title>
- <description>eee</description>
- <link>http://mikeowen.blogbus.com/logs/23972142.html</link>
- <author>mikeowen</author>
- <pubDate>Wed, 02 Jul 2008 12:26:40 +0800</pubDate>
- </item>
- </channel>
- </rss>
这是我一个同事的博客rss文件我取下来作实例了吧,下面我们来看看解析xml的方法用dom来做,实例代码如下:
- <?php
- $doc = new DOMDocument();
- $doc->load( 'wk.xml' );
- $books = $doc->getElementsByTagName( "item" );
- foreach( $books as $book )
- {
- $authors = $book->getElementsByTagName( "title" );
- $author = $authors->item(0)->nodeValue;
- $publishers = $book->getElementsByTagName( "link" );
- $publisher = $publishers->item(0)->nodeValue;
- $titles = $book->getElementsByTagName( "pubDate" );
- $title = $titles->item(0)->nodeValue;
- echo "$title - $author - $publishern";
- }
- //开源代码phpfensi.com
- ?>
简单吧,直接读取节点然后再取当前第一个节点的值就行了,好了下面我们再看看一种方法用php自然的,代码如下:
- <?php
- $g_books = array();
- $g_elem = null;
- function startElement( $parser, $name, $attrs )
- {
- global $g_books, $g_elem;
- if ( $name == 'item' ) $g_books []= array();
- $g_elem = $name;
- }
- function endElement( $parser, $name )
- {
- global $g_elem;
- $g_elem = null;
- }
- function textData( $parser, $text )
- {
- global $g_books, $g_elem;
- if ( $g_elem == 'link' ||
- $g_elem == 'pubDate' ||
- $g_elem == 'title' )
- {
- $g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text;
- }
- }
- $parser = xml_parser_create();
- xml_set_element_handler( $parser, "startElement", "endElement" );
- xml_set_character_data_handler( $parser, "textData" );
- $f = fopen( 'wk.xml', 'r' );
- while( $data = fread( $f, 4096 ) )
- {
- xml_parse( $parser, $data );
- }
- xml_parser_free( $parser );
- foreach( $g_books as $book )
- {
- echo $book['title']." - ".$book['link']." - ";
- echo $book['pubDate']."n";
- }
- ?>
这种代码多一点,单效率要比上面那个高很多的.
Tags: php读取xml xml列表程序
- 上一篇:PHP和AJAX的RSS阅读器
- 下一篇:php怎么取读xml文件
相关文章
- ·PHP如何读取xml文件?php读取xml文档例子(2014-08-20)
- ·php读取xml文件的三种实现方法(2014-08-20)
- ·php读取树型xml文件实现类(2014-08-20)
- ·php读取xml文件(2014-08-21)
- ·如何在PHP中读取XML文件(2019-12-30)
- ·PHP 读取和编写 XML(2021-04-27)
- ·PHP中使用xmlreader读取xml数据示例(2021-05-05)
- ·PHP读取XML文件的方法实例总结【DOMDocument及simplexml方法】(2021-12-16)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)