Page 1 of 1

SimpleXML A Basic Introduction to Manipulating XML Output Rate Topic: -----

#1 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Posted 27 February 2009 - 03:08 AM

With the increasing popularity of XML as an API output for a variety of online games, application programmers need a way for their scripts to effectively parse the returned data. This tutorial will instruct readers in the basic operation of PHP's SimpleXML extension.

What you will learn in this tutorial:
- How SimpleXML reads data into variable form.
- How to access attributes of an XML tag with SimpleXML.
- A little bit about inline typecasting.
- Probably a couple of other things too.

To demonstrate how this works, we'll start out with some basic XML output. The code we'll be using was posted recently in a question by hrvoje89, and is output from ASBIS's API. Here is the XML document:

<Info>
	<map>
		<element>
			<key>LINKS_TYPE</key>
			<value>
				<list>
					<element SortOrder="0" Text="Specification" Source="http://eu.computers.toshiba-europe.com/cgi-bin/ToshibaCSG/jsp/productPage.do?service=EU&PRODUCT_ID=150493" InfoTypeId="2" />
				</list>
			</value>
		</element>
		<element>
			<key>VENDORS_LINKS_TYPE</key>
			<value>
				<list />
			</value>
		</element>
		<element>
			<key>BENIFITS_FEATURES_TYPE</key>
			<value>
				<list />
			</value>
		</element>
		<element>
			<key>MARKETING_TYPE</key>
			<value>
				<list>
					<element SortOrder="0" Text="The Satellite A300 has the widest range of models available so you get to choose the machine configurations that are perfect for your needs and your pocket. The laptop features a new, striking and highly durable design using the Toshiba special glossy Fusion finish. A fully integrated Touch Pad ensures maximum user comfort." Source="" />
				</list>
			</value>
		</element>
	</map>
</Info>


The following is a small piece of code that could be used to read through this XML, and the output of SimpleXML's read-through of said XML file. For the purposes of this tutorial, we'll assume the XML file is contained in the same directory as our script, and is called "file.xml".

<?php

$xml = simplexml_load_file("file.xml");

print_r($xml);

?>


The code above reads the file into the $xml variable, and outputs the following (formatted for ease of viewing).

SimpleXMLElement Object
(
	[map] => SimpleXMLElement Object
		(
			[element] => Array
				(
					[0] => SimpleXMLElement Object
						(
							[key] => LINKS_TYPE
							[value] => SimpleXMLElement Object
								(
									[list] => SimpleXMLElement Object
										(
											[element] => SimpleXMLElement Object
												(
													[@attributes] => Array
														(
															[SortOrder] => 0
															[Text] => Specification
															[Source] => http://eu.computers.toshiba-europe.com/cgi-bin/ToshibaCSG/jsp/productPage.do?service=EU&PRODUCT_ID=150493
															[InfoTypeId] => 2
														)

												)

										)

								)

						)

					[1] => SimpleXMLElement Object
						(
							[key] => VENDORS_LINKS_TYPE
							[value] => SimpleXMLElement Object
								(
									[list] => SimpleXMLElement Object
										(
										)

								)

						)

					[2] => SimpleXMLElement Object
						(
							[key] => BENIFITS_FEATURES_TYPE
							[value] => SimpleXMLElement Object
								(
									[list] => SimpleXMLElement Object
										(
										)

								)

						)

					[3] => SimpleXMLElement Object
						(
							[key] => MARKETING_TYPE
							[value] => SimpleXMLElement Object
								(
									[list] => SimpleXMLElement Object
										(
											[element] => SimpleXMLElement Object
												(
													[@attributes] => Array
														(
															[SortOrder] => 0
															[Text] => The Satellite A300 has the widest range of models available so you get to choose the machine configurations that are perfect for your needs and your pocket. The laptop features a new, striking and highly durable design using the Toshiba special glossy Fusion finish. A fully integrated Touch Pad ensures maximum user comfort.
															[Source] => 
														)

												)

										)

								)

						)

				)

		)

)


As you an see, the output is formatted as type "SimpleXMLElement Object", and is read similarly to an array, with a couple of important differences. Let's say for example you wanted to output the value of the key tag from the first element tag (0). You would use this code to do so:

echo $xml->map->element[0]->key;

That code will output "LINKS_TYPE". You'll notice that $xml is of SimpleXMLElement Object type, so contents of it are read via a single arrow (->). $xml->element would point to the array of all the element tags at that level of the XML document (hence the [0], as we're browsing an array). It's important to remember that each individual tag will be a SimpleXMLElement object, while if you have multiple tags of the same name at the same level, they'll be a numerically-indexed array containing the SimpleXMLElement Objects the tags are placed into.

But that leaves a question, then. How would we go about getting data out of a tag's attributes? You'll note the attributes are in an array called "@attributes", but you can't access them that way (you'll get a syntax error: unexpected '@'). SimpleXML has a function designed specifically for this task: attributes(). So, if you wanted to pull the attributes of the first element tag within a list tag, you'd use this:

print_r($xml->map->element[0]->value->list->attributes());

Bear in mind this outputs a SimpleXMLObject containing an array called @attributes when you use this.

But what if you only wanted one attribute? We'll use the Text attribute for this example. You'd run it just like you would for the Objects earlier. So, it'd be:

echo $xml->map->element[0]->value->list->attributes()->Text;

The line above would result in "Specification" being output.

Now, let's have a look at a full set of code using everything I've covered here.

<?php

//Load your XML file.
$object = simplexml_load_file("file.xml");

//Create your array for elements to be stored in.
$elemarray = Array();

//Cycle through each element tag..
for($i=0; $object->map->element[$i]; $i++) {
	// Look to see if an internal element tag is set.
	if(isset($object->map->element[$i]->value->list->element)) {
		//If it is, grab the attributes of it with SimpleXML's attributes() function, and store that data in our element array.
		foreach($object->map->element[$i]->value->list->element->attributes() as $key => $val) {
			$elemarray[$i][$key] = (string)$val;
		}
	}
}

//Re-index the array.
$elemarray = array_values($elemarray);

//Display the data as stored by PHP.
echo "<pre>";
print_r($elemarray);
echo "</pre>";

?>


The code above is designed to pull the attributes of every element tag nested within a list tag, and to drop them into an array, and output them for the user to see, so they can edit the code later to manipulate it as they need to. It is commented to explain the steps it's taking.

You'll notice, though, a line that's using inline typecasting:

$elemarray[$i][$key] = (string)$val;

The reason for this inline typecasting is because, due to how SimpleXML collects the data, it's typecasted as SimpleXMLElement Object, so if we were to get it without typecasting, the results would be this:

Array
(
	[0] => Array
		(
			[SortOrder] => SimpleXMLElement Object
				(
					[0] => 0
				)

			[Text] => SimpleXMLElement Object
				(
					[0] => Specification
				)

			[Source] => SimpleXMLElement Object
				(
					[0] => http://eu.computers.toshiba-europe.com/cgi-bin/ToshibaCSG/jsp/productPage.do?service=EU&PRODUCT_ID=150493
				)

			[InfoTypeId] => SimpleXMLElement Object
				(
					[0] => 2
				)

		)

	[1] => Array
		(
			[SortOrder] => SimpleXMLElement Object
				(
					[0] => 0
				)

			[Text] => SimpleXMLElement Object
				(
					[0] => The Satellite A300 has the widest range of models available so you get to choose the machine configurations that are perfect for your needs and your pocket. The laptop features a new, striking and highly durable design using the Toshiba special glossy Fusion finish. A fully integrated Touch Pad ensures maximum user comfort.
				)

			[Source] => SimpleXMLElement Object
				(
					[0] => 
				)

		)

)


It's a bit messy that way, so we do inline typecasting to clean our output up and make it more manageable. The output of the full code above, including inline typecasting, is this:

Array
(
	[0] => Array
		(
			[SortOrder] => 0
			[Text] => Specification
			[Source] => http://eu.computers.toshiba-europe.com/cgi-bin/ToshibaCSG/jsp/productPage.do?service=EU&PRODUCT_ID=150493
			[InfoTypeId] => 2
		)

	[1] => Array
		(
			[SortOrder] => 0
			[Text] => The Satellite A300 has the widest range of models available so you get to choose the machine configurations that are perfect for your needs and your pocket. The laptop features a new, striking and highly durable design using the Toshiba special glossy Fusion finish. A fully integrated Touch Pad ensures maximum user comfort.
			[Source] => 
		)

)


This has been a basic tutorial introducing you to the power of SimpleXML. There is a lot more to learn, but you now are aware of the basics for operating it successfully, and getting the most out of XML output. Thanks for reading.

This post has been edited by Valek: 27 February 2009 - 12:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: SimpleXML

#2 HerofTime  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 29-July 09

Posted 30 August 2009 - 09:43 PM

Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1