Académique Documents
Professionnel Documents
Culture Documents
H
F-XC A N G
a dynamic Google sitemap using the PHP5 SimpleXML extension Page F1-XCof
HAN 4
G
PD E PD E
!
W
W
O
O
N
N
y
y
bu
bu
to
to
k
k
lic
lic
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u-tr a c k c u-tr a c k
Topics Article
CSS Creating a dynamic Google sitemap using the PHP5 SimpleXML extension
JavaScript
Linux Author: Gregory Boshoff
Previous versions of PHP lacked easy to use native XML parsing functions. The
Search
SimpleXML extension which is enabled by default as of PHP5 goes some way toward
addressing this short coming.
Keyword(s):
search term
SimpleXML is designed to easy the use of working with simple XML structures.
Options: SimpleXML represents a XML file as a native PHP object and this makes it a good deal
Directory easier to work with than either DOM or SAX. SimpleXML works by parsing XML into a
Search
tree like structure in which each XML tag is an object property whose contents are
accessible. Using SimpleXML you can read, write or iterate over a XML file and access
Login it's elements and attributes.
Username: However, with the PHP SimpleXML extension you can do more than carry out basic
manipulations of a XML files elements or attributes. PHP5 also has a mechanism to
Enter username convert XML nodes between SimpleXML and the more powerful PHP5 DOM extension
Password: via the simplexml_import_dom function, which extracts a SimpleXMLElement object
from a node of a DOM document.
Register | Forgot Whilst there are free online sitemap generator tools available on the Internet that allow
you to create an XML sitemap which can be submitted to Google sitemaps. The majority
of these tools work by attempting to spider a entire website and this tends not to be very
accurate.
http://www.phpfive.net/article34.htm 5/13/2007
Creating
H
F-XC A N G
a dynamic Google sitemap using the PHP5 SimpleXML extension Page F2-XCof
HAN 4
G
PD E PD E
!
W
W
O
O
N
N
y
y
bu
bu
to
to
k
k
lic
lic
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u-tr a c k c u-tr a c k
In order to better demonstrate the SimpleXML extension to you, we will do something
useful and create a PHP function that will parse and prepend new entries dynamicly to a
web sites Google sitemap.
Site maps as I'm sure the majority of you will already know help to avoid search engine
indexing problems by letting search engine spiders know what URLs are available on
your site and how often they change.
A Google XML site map is made up of URL elements which acts as a container for the
following four child elements.
Hence a single sitemap entry for this web sites home page would look something like
this.
Each time you wish to add a new web page for indexing with Google this file must first
be amended. Naturally on a large dynamic web site this is not a task that one would
wish to undertake manually. The function below automates this task and should be
called when ever you insert a item that you wish to be indexed into your web sites
database.
<?php
/**
* Add elements to Google sitemaps.
*
* SimpleXMLElement addChild method requires PHP > 5.1.3
* If you are using a version of PHP earlier than this use you will need to the DOM extention instead.
* File permissions: Your sitemaps xml file must be writable.
*/
function prepend_sitemap ($url ='' , $sitemap_file ='sitemap.xml', $priority ='0.5' , $changefreq ='weekly')
{
http://www.phpfive.net/article34.htm 5/13/2007
Creating
H
F-XC A N G
a dynamic Google sitemap using the PHP5 SimpleXML extension Page F3-XCof
HAN 4
G
PD E PD E
!
W
W
O
O
N
N
y
y
bu
bu
to
to
k
k
lic
lic
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u-tr a c k c u-tr a c k
// We are now able to manipulate the object ($go) and add a new url child element to it.
$sitemap = $go ->addChild( 'url');
/*
You may also add attributes to XML files.
For example:
$sitemap->addAttribute('total', '1.40');
Would add a total attribute with a value of 1.40 to the url element.
<url total="1.40">
*/
return true;
} // end fun
?>
If we where to run the script above the resulting modified sitemap.xml file would look
something like this:
http://www.phpfive.net/article34.htm 5/13/2007
Creating
H
F-XC A N G
a dynamic Google sitemap using the PHP5 SimpleXML extension Page F4-XCof
HAN 4
G
PD E PD E
!
W
W
O
O
N
N
y
y
bu
bu
to
to
k
k
lic
lic
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u-tr a c k
<priority>0.5</priority> c u-tr a c k
<lastmod>2006-05-14</lastmod>
<changefreq>monthly</changefreq>
</url>
</urlset>
http://www.phpfive.net/article34.htm 5/13/2007