Logo         Community
The Company
Your Account
Contact Us
PHP 101 (part 11): Sinfully Simple
Change your life with SimpleXML

| Petting Zoo |

To see how SimpleXML works, consider the following XML file:

<?xml version="1.0"?>
    <name>Polly Parrot</name>
        <mother>Pia Parrot</mother>
        <father>Peter Parrot</father>

Now, you need a way to get to the content enclosed between the <name>, <age>, <species> and <parents> elements. With SimpleXML, it's a snap:

// set name of XML file
$file = "pet.xml";

// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");

// access XML data
echo "Name: " . $xml->name . "\n";
echo "Age: " . $xml->age . "\n";
echo "Species: " . $xml->species . "\n";
echo "Parents: " . $xml->parents->mother . " and " .  $xml->parents->father . "\n";

The action begins with the simplexml_load_file() function, which accepts the path and name of the XML file to be parsed. The result of parsing the file is a PHP object, whose properties correspond to the elements under the root element. The character data within an element can then be accessed using standard object->property notation, beginning with the root element and moving down the hierarchical path of the document.

Just as you can read, so also can you write. SimpleXML makes it easy to alter the contents of a particular XML element - simply assign a new value to the corresponding object property. Here's an example:

// set name of XML file
$file = "pet.xml";

// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");

// modify XML data
$xml->name = "Sammy Snail";
$xml->age = 4;
$xml->species = "snail";
$xml->parents->mother = "Sue Snail";
$xml->parents->father = "Sid Snail";

// write new data to file
file_put_contents($file, $xml->asXML());

Here, the original XML file is first read in, and then the character data enclosed within each element is altered by assigning new values to the corresponding object property. The asXML() method, typically used to dump the XML tree back out to the standard output device, is in this instance combined with the file_put_contents() function to overwrite the original XML document with the new data.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback