Parsing XML data in PHP_PHP tutorial

WBOY
Release: 2016-07-22 09:02:20
Original
1008 people have browsed it

If you plan to process XML data in PHP, you will need an XML library to extract the data for you. Examples include parsing RSS feeds or pattern matching (looking for XHTML images or elements).

SimpleXML extension provides a very intuitive API, making it easier to convert XML into objects and traverse elements. The only drawback is that loading the entire document in memory or very large XML files may be a performance issue.

If performance is a consideration, you can go for XMLReader. XMLReader is an XML parser that loads the entire document in memory instead of by iterating through each node during the loading process.

The code below uses simple XML to get the latest RSS from my website. On my server I've used the Curl library to handle HTTP connections since it supports server hosting and is more secure.

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li><span>   </span></li><li class="alt"><span>function load_file($url) {  </span></li><li><span>$</span><span class="attribute">ch</span><span> = </span><span class="attribute-value">curl_init</span><span>($url);  </span></li><li class="alt"><span>#Return http response in string  </span></li><li><span>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  </span></li><li class="alt"><span>$</span><span class="attribute">xml</span><span> = </span><span class="attribute-value">simplexml_load_string</span><span>(curl_exec($ch));  </span></li><li><span>return $xml;  </span></li><li class="alt"><span>}  </span></li><li><span>   </span></li><li class="alt"><span>$</span><span class="attribute">feedurl</span><span> = </span><span class="attribute-value">'http://naveenbalani.com/index.php/feed/'</span><span>;  </span></li><li><span>$</span><span class="attribute">rss</span><span> = </span><span class="attribute-value">load_file</span><span>($feedurl);  </span></li><li class="alt"><span>   </span></li><li><span>foreach ($rss-</span><span class="tag">></span><span>channel-</span><span class="tag">></span><span>item as $item) {  </span></span></li>
<li class="alt">
<span>echo "</span><span class="tag"><</span><span class="tag-name">h2</span><span class="tag">></span><span>" . $item-</span><span class="tag">></span><span>title . "</span><span class="tag"></</span><span class="tag-name">h2</span><span class="tag">></span><span>";  </span>
</li>
<li>
<span>echo "</span><span class="tag"><</span><span class="tag-name">p</span><span class="tag">></span><span>" . $item-</span><span class="tag">></span><span>description . "</span><span class="tag"></</span><span class="tag-name">p</span><span class="tag">></span><span>";  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>   </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

http://css.dzone.com/news/parsing-xml-data-php


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445843.htmlTechArticle If you plan to process XML data in PHP, you'll need an XML library to extract the data for you. Examples include parsing RSS feeds or pattern matching (looking for XHTML images or elements). The SimpleXML extension provides a...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template