The introduction to XML seems pretty well covered guys so this document will talk more about working will XML directly with PHP to begin with

 

What is Simple XML

Simple XML are functions that are part of PHP that allows users to easily manipulate/use XML data. Simple XML is a great way to either render XML or consume it, using the built in extension with PHP a developer can begin working with XML in a matter of minutes.

www.flatpackhouses.co.uk

How do I know if I have Simple XML installed?

On some hosting packages Simple XML may not be installed/compiled with your version of PHP. As of PHP version 5 Simple XML is core functionality, but if you are unsure there are a few ways you can find out whether you have Simple XML.

  • Browser – Create an empty PHP file called phpinfo.php. Inside this file input the following code:

1

  • Upload this file to your server or localhost
  • Input the full URL into your browser address bar ( http://www.example.com/phpinfo.php )
  • Once complete your browser will list all enable extensions with PHP, scroll down and see if you can locate “Simple XML”. If it’s present you are ready to begin working with XML. If Simple XML is not present you can follow the steps below to ensure it is installed. XML is usually installed as standard with PHP

 

WAMP

WAMP server includes the latest version of PHP as standard, WAMP users should be able to work with Simple XML without any need for additional configuration.

 

Web Server

Please only carry out the following if you have experience working with servers at the command line in SSH. Perform the following to ensure PHP-XML is installed:

  • SSH into your server.
  • su – – root to ensure you have a user with elevated permissions.
  • Depending on your distro one of the following will work:
    YUM
    – sudo yum install php-xml
    APTITUDE
    - sudo apt install php-xml. This could also be: sudo apt-get install php-xml
    Depending on your distro you may ;/need to add additional repos to get the correct version of PHP-XML, for example when working with CentOs you may need to add the latest EPEL repo to your distro.
  • Once you have installed the extension, you will need to restart the Apache HTTP Daemon, you can achieve this by running:service httpd restartThis may be different on your distro, generally the format of the call is the same.

Once the above is complete you can be rest assured you are ready to work with Simple XML

 

How to use Simple XML

Below are a few working examples of how we render XML to the browser, followed by an example of how we can consume an XML RSS feed. The examples listed below covers a basic introduction in how to work with Simple XML. Mozbot

 

Example 1 – Using PHP to render data from RSS feeds.

We will be using BBC’s RSS feed http://feeds.bbci.co.uk/news/rss.xml” in our example.

Create a file called simplexml.php and add this snippet .

 

<code>

<?php $xml = simplexml_load_file(“http://feeds.bbci.co.uk/news/rss.xml”);

echo “<pre>”;

print_r($xml);

echo “</pre>”;

?>

</code>

 

What this does is displays the different nodes/data in the xml file provided.

 

2

 

Now we can tap into anything in this file and render what we want by doing some basic PHP.

An example is if we wanted to show the title of the RSS feed which would be “BBC News – Home

Copy and paste the snippet below.

<code>

<?php

$xml = simplexml_load_file(“http://feeds.bbci.co.uk/news/rss.xml”);

echo $xml->channel->title;

?>

</code>

Ta-da, you have just rendered data from an XML feed. The most important bit of code is this php function “simplexml_load_file()”. More information can be found here (link http://php.net/manual/en/function.simplexml-load-file.php), but the basic concept is that allows you to use XML as an object. What “$xml->channel->title;” does is gets access to the RSS feed object. Now if we wanted to get a list of the 5 most recent entries in the feed we would need to create a basic loop and echo out what we needed. Paste the snippet below

<code>

<?php

$xml = simplexml_load_file(“http://feeds.bbci.co.uk/news/rss.xml”);

echo “<h1>”.$xml->channel->title.”</h1>”;

for ($number_posts = 0; $number_posts <= 4; $number_posts++) {

$item = $xml->channel->item[$number_posts];

 

echo “<h2>”.$item->title.”</h2>”;

}

?>

</code>

Congratulations we have now used simple xml successfully.

 

Example 2

Below is an example of a XML order feed for an ecommerce website, the requirement for this functionality was that each order was wrapped in well-formed XML  to allow a stock system to received order updates.

This XML feed for specifically created for Magento, the same format could be used for any system. Using this working example will help you to better understand how we work with XML. Ple

 

3