<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>某人的栖息地 &#187; xml</title>
	<atom:link href="http://www.ooso.net/tag/xml/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ooso.net</link>
	<description>Linux + Apache + Mysql + Php + Flash</description>
	<lastBuildDate>Thu, 19 Jan 2012 01:21:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>[php5]将xml转换成json最简单的办法</title>
		<link>http://www.ooso.net/archives/361</link>
		<comments>http://www.ooso.net/archives/361#comments</comments>
		<pubDate>Tue, 24 Jul 2007 00:18:58 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[simplexml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.ooso.net/index.php/archives/361</guid>
		<description><![CDATA[在php5下，将xml转换成json最简单的办法，就是利用simplexml和json扩展。
废话不多说，贴代码:
&#60;?php
$xml = &#60;&#60;&#60;EOF

				<span class="readmore"><a href="http://www.ooso.net/archives/361" title="[php5]将xml转换成json最简单的办法">阅读全文（114字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>在php5下，将xml转换成json最简单的办法，就是利用simplexml和<a href="/index.php?tag=json">json</a>扩展。</p>
<p>废话不多说，贴代码:</p>
<pre><code>&lt;?php
$xml = &lt;&lt;&lt;EOF
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;books&gt;
    &lt;book id="1"&gt;
        &lt;title&gt;Code Generation in Action&lt;/title&gt;
        &lt;author&gt;&lt;first&gt;Jack&lt;/first&gt;&lt;last&gt;Herrington&lt;/last&gt;&lt;/author&gt;
        &lt;publisher&gt;Manning&lt;/publisher&gt;
    &lt;/book&gt;

    &lt;book id="2"&gt;
        &lt;title&gt;PHP Hacks&lt;/title&gt;
        &lt;author&gt;&lt;first&gt;Jack&lt;/first&gt;&lt;last&gt;Herrington&lt;/last&gt;&lt;/author&gt;
        &lt;publisher&gt;O'Reilly&lt;/publisher&gt;
    &lt;/book&gt;

    &lt;book id="3"&gt;
        &lt;title&gt;Podcasting Hacks&lt;/title&gt;
        &lt;author&gt;&lt;first&gt;Jack&lt;/first&gt;&lt;last&gt;Herrington&lt;/last&gt;&lt;/author&gt;
        &lt;publisher&gt;O'Reilly&lt;/publisher&gt;
    &lt;/book&gt;
&lt;/books&gt;
EOF;

echo $json = json_encode(simplexml_load_string($xml));
?&gt;</code></pre>
<p>整个过程就是一行，灰墙之ez.</p>
<p>另：可以跟以前写的“<a href="http://www.ooso.net/index.php/archives/184">用php将rss转化为json格式</a>”比较一下.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/361/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP5的Simplexml</title>
		<link>http://www.ooso.net/archives/258</link>
		<comments>http://www.ooso.net/archives/258#comments</comments>
		<pubDate>Wed, 11 Oct 2006 23:37:19 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.ooso.net/index.php/archives/258</guid>
		<description><![CDATA[php5新增了Simplexml extension,我们可以借助它来解析,修改XML。在IBM的知识库里找到一篇文章对此做了专门的介绍，而且比较详细，感兴趣的话可以看看最后的参考文档。
一个RSS Feed
下面是一个RSS的例子，我们准备用simplexml来解析它。
[xml]

				<span class="readmore"><a href="http://www.ooso.net/archives/258" title="PHP5的Simplexml">阅读全文（1664字）</a></span>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.ooso.net/?tag=php5">php5</a>新增了Simplexml extension,我们可以借助它来解析,修改XML。在IBM的知识库里找到一篇文章对此做了专门的介绍，而且比较详细，感兴趣的话可以看看最后的参考文档。</p>
<h3>一个RSS Feed</h3>
<p>下面是一个RSS的例子，我们准备用simplexml来解析它。<br />
[xml]<?xml version="1.0" encoding="UTF-8"?><br />
<rss version="0.92"><br />
<channel></p>
<link>http://www.elharo.com/blog</link>
  <language>en</language><br />
  <item></p>
<p>    <description><br />
     The old Penn Station in New York was torn down before I was born.<br />
     Looking at these pictures, that feels like a mistake.  The current site is<br />
     functional, but no more; really just some office towers and underground<br />
     corridors of no particular interest or beauty. The new Madison Square&#8230;<br />
    </description></p>
<link>http://www.elharo.com/blog/new-york/2006/07/31/penn-station</link>
  </item><br />
  <item></p>
<p>    <description>Some people use very obnoxious spam filters that require you<br />
     to type some random string in your subject such as E37T to get through.<br />
     Needless to say neither I nor most other people bother to communicate with<br />
     these paranoids. They are grossly overreacting to the spam problem.<br />
     Personally I won&#8217;t &#8230;</description></p>
<link>http://www.elharo.com/blog/tech/2006/07/28/personal-for-elliotte-harold/</link>
  </item><br />
</channel><br />
</rss>[/xml]</p>
<h3>解析XML</h3>
<p>首先载入一个<a href="http://www.ooso.net/?tag=xml">xml</a></p>
<pre><code>$rss =  simplexml_load_file('http://www.ooso.net/index.php/feed/');</code></pre>
<p><span id="more-258"></span></p>
<p>这里使用的是simplexml_load_file函数，能够马上解析指定url的xml文件，因为是simplexml，所以simple。下面就可以象读取php数组一样来使用解析后xml的内容了，比如读取RSS的标题：</p>
<pre><code>$title =  $rss-&gt;channel-&gt;title;
&lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;</code></pre>
<p>或者是循环显示rss的各个ITEM节点</p>
<pre><code>$rss-&gt;channel-&gt;item //这个是item</code></pre>
<pre><code>foreach ($rss-&gt;channel-&gt;item as $item) {
  echo "&lt;h2&gt;" . $item-&gt;title . "&lt;/h2&gt;";
  echo "&lt;p&gt;" . $item-&gt;description . "&lt;/p&gt;";
}</code></pre>
<h3>一个简单但完整的RSS Reader</h3>
<p>把上面的代码整合在一起，就是一个五脏俱全的麻雀牌RSS Reader了。</p>
<pre><code>&lt;?php
// 载入并解析XML
$rss =  simplexml_load_file('http://partners.userland.com/nytRss/nytHomepage.xml');
$title =  $rss-&gt;channel-&gt;title;
?&gt;
&lt;html xml:lang="en" lang="en"&gt;
&lt;head&gt;
  &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;&lt;?php echo $title; ?&gt;&lt;/h1&gt;

&lt;?php
// 循环输出ITEM节点的说明
foreach ($rss-&gt;channel-&gt;item as $item) {
  echo "&lt;h2&gt;&lt;a href='" . $item-&gt;link . "'&gt;" . $item-&gt;title . "&lt;/a&gt;&lt;/h2&gt;";
  echo "&lt;p&gt;" . $item-&gt;description . "&lt;/p&gt;";
}
?&gt;

&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>Simplexml,真的很simple,不信可以拿去和<a href="http://www.ooso.net/index.php/archives/category/php/">php</a>的DOM function做下比较:)</p>
<h3>参考</h3>
<p><a href="http://www-128.ibm.com/developerworks/xml/library/x-simplexml.html">http://www-128.ibm.com/developerworks/xml/library/x-simplexml.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/258/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

