Simple XML in PHP Tutorial

Simple XML Parsing in PHP

The Boring Introduction
Parsing XML in PHP can be really confusing at times; but what about if we only want to do something really simple? SIMPLE XML to the RESCUE!!!

Simple XML, as it’s name surgests is a really easy way to access XML elements through PHP.

How does it work?
Well, first we start the PHP document…
<?php

Now we create the instance and assign it to a variable…
$xml = new SimpleXMLElement(“<?xml version=’1.0’…. blah blah blah”);

That can be really usefull if we want to write the XML inside the PHP document – but what if we don’t?
$xml = new simplexml_load_file(“myfile.xml”);

Great, now we’ve opened up a nice xml file and now we want to suck it dry! Firstly we need to know what the XML file contains. :/
<?xml version=”1.0″?>
<street>
<house>
<bedrooms>3</bedrooms>
<toilets status=”true”>365</toilets>
<metresofcable>2</metresofcable>
</house>
<house>
<bedrooms>4</bedrooms>
<toilets status=”false”>1</toilets>
<metresofcable>45</metresofcable>
</house>
</street>

Now that we know what out XML file looks like, we can start to parse it.

I want to find out how many toilets the first house has and print it out:
echo $xml->house[0]->toilets;

This would print out the number “365”. That was really simple wasn’t it? And equally if I wanted to print out the number of toilets in the second house I would write:
echo $xml->house[1]->toilets;

Explanation:
$xml is the variable to which we assigned the XML parser class so we access all the information through this.

$xml->house[0]
You will notice that the first tag in the xml document is “<street>” not “<houses>”. This is because the PHP XML parser skips the first tag in the XML document.

$xml->house[0]->toilets
This is pretty self explanitory. The tag “<toilets>” is inside the “<house>” tag and so we access the toilets tag through the house one.

As you will have noticed, we do not use quotation marks to encase the tag names. This is the standard way in which it is done.

What about if we want to print out all of the number of toilets in each of the houses in the street?
We use the foreach() function. Here is an example of it in use:

foreach($xml->house as $house){
echo $house->toilets.”<br />”;
}

This is quite simple. In the foreach function we assign the marker of each house to the $house variable. Inside the statement we can access $xml->house now as $house; so we can access the attributes as $house->toilets.

So, now we can output all of the information at once!
foreach($xml->house as $house){
echo “This house has “.$house->toilets.” toilets, “.$house->bedrooms”. bedrooms and “.$house->metresofcable.” metres of cable.<br />”;
}

Using Attributes
These are easy too….
foreach($xml->house as $house){
switch((string) $house->toilets[‘status’]) {
case ‘true’:
echo “This house has “.$house->toilets.” toilet(s)<br />”;
break;
case ‘false’:
echo “This house does not have “.$house->toilets.” toilet(s)<br />”;
break;
}
}

For the attributes of the object e.g. the status value in “status=’true'” we simply select the tag, in this case “$house->toilet” and then use it as an array to select the attribute: “$house->toilet[‘status’]”

Extra Information

When tags start to come up with names like “string” and “array” which are variables in PHP we still need to access them. This can be done with this:

echo $xml->{“array”}->house->{“string”};

The {“”} lets the simple xml function regard it as plain text and simplifies the matter.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to Simple XML in PHP Tutorial

  1. they only wanna do you dir. Andreina Cecilio.

  2. Eka Kolour says:

    you had me suicidal, suicida. Eka Kolour.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s