SimpleXML Processing in PHP

Source: Internet
Author: User

Introduction: Learn about the SimpleXML extension bundled with PHP version 5, which enables PHP pages to query, search, modify, and redistribute XML in PHP-friendly syntax.

PHP version 5 introduces SimpleXML, a new application programming interface (API) for reading and writing XML. In SimpleXML, the following expression:

$doc->rss->channel->item->title

Select the element from the document. This expression is easy to write as long as you are familiar with the structure of the document. However, if it is not clear where the required elements appear (such as Docbook, HTML, and similar narrative documents), SimpleXML can use an XPath expression to find these elements.

Start using SimpleXML

Suppose you need a PHP page to convert the RSS feed (feed) into HTML. RSS is a simple XML format for publishing chained content. The root element of the document is RSS, which includes a channel element. The channel element contains metadata about the feed, such as the title, language, and URL. It also contains a variety of reports encapsulated in the item element. Each item has a LINK element, including a URL, title or description (usually both), containing plain text. does not use namespaces. The content of RSS is more than that, but it's enough for this article to know. Listing 1 shows a typical example, which contains two news items.

Listing 1. RSS Feed

<?xml version= "1.0" encoding= "UTF-8"?>
<rss version= "0.92" >
<channel>
<title>mokka mit schlag</title>
<link>http://www.elharo.com/blog</link>
<language>en</language>
<item>
<title>penn Station:gone but not forgotten</title>
<description>
The old Penn station in New York is torn down before I was born.
Looking at these pictures, which feels like a mistake. The current site is
Functional, but no more; Really just some office towers and underground
Corridors of no particular interest or beauty. The new Madison Square ...
</description>
<link>http://www.elharo.com/blog/new-york/2006/07/31/penn-station</link>
</item>
<item>
<title>personal for Elliotte harold</title>
<description>some people use very obnoxious spam filters
To type some random string in your subject such as e37t to get through.
Needless to say neither I nor most other people bother to communicate with
These paranoids. They are grossly overreacting to the spam problem.
Personally I won ' t ...</description>
<link>http://www.elharo.com/blog/tech/2006/07/28/personal-for-elliotte-harold/</link>
</item>
</channel>
</rss>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.