Simplify your business
Monday, 13 October 2008 10:49 pm

Converting PHP arrays to XML

Thursday, 1 November 2007  

I thought I’d share with you a PHP class I found useful lately. I had to take a comma-delimited text file and turn it into an XML text stream. With the ubiquity of XML these days, you might find it useful too, if you dabble in PHP software development.

The class:

class array2xml {

   var $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

   function array2xml($array, $root = 'root', $element = 'element') {     
      $this->output .= $this->make($array, $root, $element);
   }

   function make($array, $root, $element) {
      $xml = "<{$root}>\n";
      foreach ($array as $key => $value) {
         if (is_array($value)) {
            $xml .= $this->make($value, $element, $key);
         } else {
            if (is_numeric($key)) {
               $xml .= "<{$root}>{$value}</{$root}>\n";
            } else {
               $xml .= "<{$key}>{$value}</{$key}>\n";
            }
         }
      }
      $xml .= "</{$root}>\n";      
      return $xml;
   }

}

The script:

// initialise variables
$array = array();
$i = 0;

// open the text file
$file = file('cc.txt');

// create array of country names and codes
foreach ($file as $line) {
   $line = split(',', trim($line));
   $array[$i]['code'] = $line[1];
   $array[$i]['name'] = $line[0];
   $i++;
}

// convert the array to xml output
$xml = new array2xml($array, 'countries', 'country');
echo $xml->output;

Assuming you’ve got a text file named "cc.txt", which looks like this:

Argentina,AR
Australia,AU
Croatia,HR
Italy,IT
Malta,MT
Poland,PL
Serbia,RS
Ukraine,UA

The above script will produce this as output:

<?xml version="1.0" encoding="utf-8"?>
<countries>
<country>
<code>AR</code>
<name>Argentina</name>
</country>
<country>
<code>AU</code>
<name>Australia</name>
</country>
<country>
<code>HR</code>
<name>Croatia</name>
</country>
<country>
<code>IT</code>
<name>Italy</name>
</country>
<country>
<code>MT</code>
<name>Malta</name>
</country>
<country>
<code>PL</code>
<name>Poland</name>
</country>
<country>
<code>RS</code>
<name>Serbia</name>
</country>
<country>
<code>UA</code>
<name>Ukraine</name>
</country>
</countries>

I won’t go into details about the relationship between the last two arguments of the "array2xml()" function and the XML elements because that should pretty much be obvious if you’re a programmer. If not, then you’re likely to be bored by now anyway.

Posted in PHP, Programming, Tips by Ivan
Blinklist icon Del.iocio.us icon Furl icon Reddit icon Technorati icon Yahoo! icon

Got something to say?

To protect your privacy, your email address will not be displayed.





Some basic rules for commenting:

  • Watch your language.
  • Keep comments on-topic and relevant.
  • You can use basic XHTML tags for formatting and linking but not bbcode.
  • Comments are moderated, so don't double post if your comment doesn't appear immediately.
  • Please proof-read your comments for spelling and grammar mistakes.
  • Watch your language.