Featured

Generate XML form two dimensional Array

php copy Generate XML form two dimensional Array

Genrate XML from Array – PHP

I have searched a lot for php code to generate xml from a two dimensional array. But all of them was not working perfectly.  The following code generates xml file from a two dimensional array.  This only works for the array with the following structure.

array(1) {
  ["share_friend_list"]=>;
  array(3) {
    ["your_name"]=>
    string(12) "Sarath"
    ["friends"]=>
    array(1) {
      ["friend"]=>
      array(3) {
        [0]=>
        array(2) {
          ["name"]=>
          string(6) "Ryan"
          ["msisdn"]=>
          string(9) "213412414"
        }
        [1]=>
        array(2) {
          ["name"]=>
          string(12) "Nash"
          ["msisdn"]=>
          string(9) "123423421"
        }
        [2]=>
        array(2) {
          ["name"]=>
          string(9) "Ashish"
          ["msisdn"]=>
          string(9) "123421342"
        }
      }
    }
    ["your_email"]=>
    string(19) "[email protected]"
        $filePath = $this->xmlFilePath.$this->today.' - '.$type.'.xml';
        $this->doc->formatOutput = true;
 
        if( ! file_exists( $filePath ) )
        {
               $r = $this->doc->createElement( "data" );
               $this->doc->appendChild( $r );
        }
        else
        {
            @$this->doc->load( $filePath );
            $r  = $this->doc->getElementsByTagName('data')->item(0);
        }
 
        $this->extract( $info , $r  );
 
        try
        {
 
            $this->doc->saveXML();
            $this->doc->save( $filePath );
        }
        catch ( Exception $e )
        {
 
        }
 public function extract( $info , $b )
    {
 
        if( is_array( $info ) && is_array( $info[ key($info)]) )
        {
                $temp = $b;
 
                // New parent node
                if( is_integer(  key($info) )  )
                {
                     $this->extract( $info[ key($info)], $b );
                     if( count( array_slice(  $info , 1 ) ) > 0  )
                     $b = $this->parentNode->appendChild( $this->doc->createElement( $this->parentName ));
                }
               else
                {   // Inner nodes
                    $b = $b->appendChild(  $this->doc->createElement( key($info) ) ) ;
 
                    $this->parentNode = $temp;
                    $this->parentName = key($info);
                    $this->extract( $info[ key($info)], $b );
                    $b = $temp;
 
                }
 
        }
        else
        {
            $temp = $this->doc->createElement( key($info) );
            $temp->appendChild( $this->doc->createTextNode( $info[ key($info)] ) );
 
            $b->appendChild( $temp );
 
        }
 
        $newArray = array_slice(  $info , 1 ) ;
 
        if( is_array( $newArray ) && count( $newArray ) > 0  )
        {
               $this->extract( $newArray, $b );
        }
        else
        {
            return;
        }
 
    }

Out put XML.

<!--?xml version="1.0"?-->
 
    Sarath
 
        Ryan
        213412414
 
        Nash
        123423421
 
        Ashish
        123421342
 
    [email protected]

Tagged , , , , ,

Share this to

Related Posts

Leave a reply

Your email address will not be published. Required fields are marked *

*

About
View Sarath D R's profile on LinkedIn

I am a technology consultant and an entrepreneur focused on the following technologies Java,Spring, Android, Angular, Asterisk, Symfony, Cake , Code-Igniter, Jquery , XAMPP, XHTML, AJAX, MySql, Seo, Web Design, Wordpress, iPhone development.

Today’s Tips
  • Generate Codova.jar file from Phonegap project using ant
  • Google map javascript client  to find the geographic co-ordinates  from  Address or post code
  • Update Android main UI from Webview – Phonegap Plugin
  • Jplayer is not working  with HTC One Android 4.3 version browser
  • Regular expression to validate mobile phone numbers starting with + ( plus ) – php
  • Simple pagination script for PHP
  • Useful adb ( Android Debug Bridge ) commands
  • Change the javascript files url to absolute url – Symfony 1.4
  • Set Http authentication header from Client – PHP / CURL

This site uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.This site also uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.

Close