For SimpleXML processing in PHP, read SimpleXML processing in PHP and learn the SimpleXML extension bundled with PHP version 5, it enables PHP pages to query, search, modify, and re-release XML with PHP-friendly syntax. PHP version 5 introduces SimpleXML, a new application programming for reading and writing XML.
Understand the SimpleXML extension bundled with PHP version 5, which enables PHP pages to query, search, modify, and re-release XML with PHP-friendly syntax.
PHP version 5 introduces SimpleXML, a new application programming interface (API) for reading and writing XML ). In SimpleXML, the following expression is used:
$ Doc-> rss-> channel-> item-> title
Select an element from the document. As long as you are familiar with the structure of the document, it is easy to write this expression. However, SimpleXML can use XPath expressions to find the elements that need to appear somewhere (such as Docbook, HTML, and similar narrative documents.
Start using SimpleXML
Suppose you need a PHP page to convert the RSS feed into HTML. RSS is a simple XML format used to publish chained content. The root element of the document is rss, which contains a channel element. The channel element contains metadata about the feed, such as the title, language, and URL. It also contains various reports encapsulated in the item element. Each item has a link element, including a URL, and a title or description (usually both), including common text. Do not use namespaces. Of course, RSS is more than that, but it is enough for this article to know. Listing 1 shows a typical example that contains two news items.
Listing 1. RSS feed
Mokka mit Schlag
Http://www.elharo.com/blog
En
Penn Station: Gone but not Forgotten
The old Penn Station in New York was torn down before I was born.
Looking at these pictures, that feels like a mistake. The current site is
Functional, but no more; really just some office towers and underground
Corridors of no participating interest or beauty. The new Madison Square...
Http://www.elharo.com/blog/new-york/2006/07/31/penn-station
Personal for Elliotte Harold
Some people use very obnoxious spam filters that require you
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
These paranoids. They are grossly overreacting to the spam problem.
Personally I won't...
Http://www.elharo.com/blog/tech/2006/07/28/personal-for-elliotte-harold/
Let's develop a PHP page to format the RSS feed as HTML. Listing 2 shows the basic structure of the page.
Listing 2. static structure of PHP code
<? Php // The title will be read from the RSS?>
// Here we'll put a loop to include each item's title and description
?>
Parse XML documents
The first step is to parse the XML document and save it to the variable. You only need a line of code to pass a URL to the simplexml_load_file () function:
$ Rss = simplexml_load_file ('http: // partners.userland.com/nytRss/nytHomepage.xml ');
For this example, I have filled the page from the New York Times feed of Userland (in the http://partners.userland.com/nytRss/nytHomepage.xml. Of course, you can also use any URL of other RSS feeds.
Note that although the name is simplexml_load_file (), this function actually resolves the XML document on the remote http url. But this is not the only strange thing about the function. The returned value (stored in the $ rss variable) does not point to the entire document. if you have used other APIs, such as the Document Object Model (DOM), you may expect this. Instead, it points to the root element of the document. From SimpleXML, you cannot access the content in the preface and conclusion of the document.
Search for the abstract title
The title of the entire feed (not the title of each report in the feed) is located in the title child of the rss root element channel. It is easy to find this title, as if the XML document is a serialized form of an object like rss, and its channel field itself carries a title field. Using the regular PHP object reference syntax, the statement for searching the title is as follows:
$ Title = $ rss-> channel-> title;
After finding it, you can add it to the output HTML. This is simple, as long as the $ title variable is displayed:
<? Php echo $ title;?>
This line outputs the string value of the element instead of the entire element. That is to say, the text content is written but the label is not included.
You can even skip the intermediate variable $ title:
<? Php echo $ rss-> channel-> title;?>
Because the page reuse this value in multiple places, I found it easier to store it with a variable with a clear meaning.
......