Categories

Archives

Syndication


PHP SimpleXML function

11
Sep
Posted in PHP, Programming

SimpleXML intro:
PHP4 had one pretty big drawback when you had to use external libraries to parse XML files. PHP5 had a number of XML libraries that solve this problem and one of the easiest is SimpleXML.

To test and use the function, a XML document is needed.
In this post we will use our own sitemap, located here: http://cdsrc.com/sitemap.xml.

As we write this (the document will change in future since it’s a sitemap), the document looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://cdsrc.com/wp-content/plugins/google-sitemap-generator/sitemap.xsl"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/09/sitemap.xsd"	xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<url>
		<loc>http://cdsrc.com/</loc>
		<lastmod>2007-09-10T11:31:33+00:00</lastmod>
		<changefreq>daily</changefreq>
 
		<priority>1.0</priority>
	</url>
 
	<url>
		<loc>http://cdsrc.com/programming/php-random-password-generator-function.html</loc>
		<lastmod>2007-09-10T11:31:33+00:00</lastmod>
		<changefreq>monthly</changefreq>
 
		<priority>0.3</priority>
	</url>
 
	<url>
		<loc>http://cdsrc.com/programming/php-and-database-space-saving-tips.html</loc>
		<lastmod>2007-09-09T13:06:17+00:00</lastmod>
		<changefreq>monthly</changefreq>
 
		<priority>0.2</priority>
	</url>
 
	<url>
		<loc>http://cdsrc.com/linux/linuxbiggerthanwindows.html</loc>
		<lastmod>2007-09-09T12:45:00+00:00</lastmod>
		<changefreq>monthly</changefreq>
 
		<priority>0.3</priority>
	</url>
</urlset>

The documents structure is pretty easy to understand. All URL’s has a , , and . All this variables can now easily be accessed by the simple SimpleXML function.

There are a couple of methods you can use to load the XML file, one of them are to use file_get_contents() and another one, which we will use, will be shown here:

1
2
3
4
5
6
7
8
<?php
$url_location = 'http://cdsrc.com/sitemap.xml';
$sitemap = new SimpleXMLElement($url_location,null,true);
foreach($sitemap as $url) 
{
	echo "Url: {$url->loc}<br />Lastmod: {$url->lastmod}<br />Changefreq: {$url->changefreq}</br>Priority: {$url->priority}<br /><br />";
}
?>

There are no necessary comments to this as the code is pretty simple to understand. You access the “subfields” using $url->”subfield” and the counter goes through all the posts in the whole sitemap.

If we now would execute that script, we will get this output:

Url: http://cdsrc.com/
Lastmod: 2007-09-10T11:31:33+00:00
Changefreq: daily
Priority: 1.0
 
Url: http://cdsrc.com/programming/php-random-password-generator-function.html
Lastmod: 2007-09-10T11:31:33+00:00
Changefreq: monthly
Priority: 0.3
 
Url: http://cdsrc.com/programming/php-and-database-space-saving-tips.html
Lastmod: 2007-09-09T13:06:17+00:00
Changefreq: monthly
Priority: 0.2
 
Url: http://cdsrc.com/linux/linuxbiggerthanwindows.html
Lastmod: 2007-09-09T12:45:00+00:00
Changefreq: monthly
Priority: 0.3

Thats a very great and easy way of using the “new” SimpleXML PHP 5.0 function!

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Technorati
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Furl
  • Netscape
  • De.lirio.us

Leave us a comment