SimpleXML processing in PHP

Source: Internet
Author: User
Keywords SimpleXML processing in PHP

Learn about the Simplexml extension bundled with PHP version 5, which enables PHP pages to query, search, modify, and republish 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 an element from the document. It is easy to write this expression 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 XPath expressions to find these elements.

Getting Started with SimpleXML

Suppose a PHP page is needed to convert an RSS feed into HTML. RSS is a simple XML format for publishing cascading 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 articles encapsulated in the item element. Each item has a LINK element, including a URL, and a title or description (usually both), containing plain text. Do not use namespaces. The content of RSS is of course more than that, but it's enough for this article to know. Listing 1 shows a typical example that contains two news items.

Listing 1. RSS Feed




<title>Mokka mit Schlag</title>
Http://www.elharo.com/blog
en

  Penn Station:gone but not forgotten
 
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 ...
 
  Http://www.elharo.com/blog/new-york/2006/07/31/penn-station


  Personal for Elliotte Harold
  Some people use very obnoxious spam filters this require you
To type some random string of your subject such as e37t to get through.
Needless to say neither I nor the other people bother to communicate with
These paranoids. They is 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 that formats the RSS feed as HTML. Listing 2 shows the basic structure of this page.

Listing 2. Static structure of PHP code




<?php//The title would be read from the RSS?>

Here we'll put a loop to include each item ' s title and description
?>




Parsing XML Documents

The first step is to parse the XML document and save it in the variable. You only need one 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 populated the page from Userland's New York times feed (in Http://partners.userland.com/nytRss/nytHomepage.xml). Of course, you can also use any URL for other RSS feeds.

Note that although the name is Simplexml_load_file (), the function actually resolves the XML document on the remote HTTP URL. But that's not the only weird thing about the function. The return value (stored here in the $RSS variable) does not point to the entire document, and you might expect this if you have used other APIs such as the Document Object Model (DOM). Instead, it points to the root element of the document. The contents of the prologue and epilogue sections of the document cannot be accessed from SimpleXML.

Find a feed title

The title of the entire feed (not the title of each article in the feed) is in the title child of the RSS root channel. It is easy to find this title, as if the XML document is a serialized form of an object of class RSS, and its channel field itself has a title field. Using the regular PHP object reference syntax, the following statement looks for the header:

$title = $rss->channel->title;


It can be added to the output HTML after it is found. This is simple, as long as you echo $title variable:

<?php echo $title;?>


This line outputs the string value of the element instead of the entire element. This means that the text content is written but does not include tags.

You can even skip the intermediate variable $title completely:

<?php Echo $rss->channel->title;?>


Because the page reuses this value in multiple places, I find it more convenient to store it with a variable that has a definite meaning.

......

  • 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.