top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to convert xml string into array in php?

+1 vote
332 views
How to convert xml string into array in php?
posted Jan 28, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

AN XML STRING

<movies>
<movie>
<title>Fist Full Of Dollars</title>
<actor>Clint Eastwood</actor>
<actor>Marianne Koch</actor>
<actor> Wolfgang Lukschy</actor>
<director>Sergio Leone</director>
<year>1964</year>
</movie>

<movie>
<title>Fist Full Of Dollars</title>
<actor>Clint Eastwood</actor>
<actor>Klaus Kinski</actor>
<director>Sergio Leone</director>
<year>1965</year>
</movie>

<movie>
<title>The Good The Bad and the Ugly</title>
<actor>Clint Eastwood</actor>
<actor>Lee Van Cleef</actor>
<actor>Eli Wallach</actor>
<director>Sergio Leone</director>
<year>1966</year>
</movie>
</movies>

THE CODE

<?php

class simpleXml2Array
{
    public $namespaces, $arr;

    public function __construct( $xmlstring, $namespaces=null )
    {
        $xml = new simpleXmlIterator( $xmlstring, null );
        $this->namespaces = is_null( $namespaces ) ? null : $xml->getNamespaces( true );
        $this->arr = $this->xmlToArray( $xml, $namespaces );

    }

    /**
     *
     * @access    public
     * @param    simpleXmlIterator    $xmlstring
     * @param    array            $namespaces
     * @return    array
     *
     */
    public function xmlToArray( $xml, $namespaces=null )
    {
        $a = array();
        $xml->rewind();
        while( $xml->valid() )
        {
            $key = $xml->key();
            if( !isset( $a[$key] ) ) 
            {
                $a[$key] = array(); $i=0; 
            }
            else
            {
                $i = count( $a[$key] );
            }
            $simple = true;
            foreach( $xml->current()->attributes() as $k=>$v ) 
            {
                $a[$key][$i][$k]=(string)$v;
                $simple = false;
            }

            if( $this->namespaces ) 
            {
                foreach( $this->namespaces as $nid=>$name ) 
                {
                    foreach( $xml->current()->attributes( $name ) as $k=>$v ) 
                    {
                        $a[$key][$i][$nid.':'.$k] =( string )$v;
                        $simple = false;
                    }
                }
            } 
            if( $xml->hasChildren() ) 
            {
                if( $simple ) $a[$key][$i] = $this->xmlToArray( $xml->current(), $this->namespaces );
                else $a[$key][$i]['content'] = $this->xmlToArray( $xml->current(), $this->namespaces);
            } 
            else 
            {
                if($simple) 
                {
                    $a[$key][$i] = strval( $xml->current() );
                }
                else 
                {
                    $a[$key][$i]['content'] = strval( $xml->current() );
                }
            }
            $i++;
            $xml->next();
        }
        return $a;
    }

} // end of class

USAGE

<?php
$xml = new simpleXml2Array( $xmlstring, null );

print_r( $xml->arr );

RESULT
Array
(
    [movie] => Array
        (
            [0] => Array
                (
                    [title] => Array
                        (
                            [0] => Fist Full Of Dollars
                        )

                    [actor] => Array
                        (
                            [0] => Clint Eastwood
                            [1] => Marianne Koch
                            [2] =>  Wolfgang Lukschy
                        )

                    [director] => Array
                        (
                            [0] => Sergio Leone
                        )

                    [year] => Array
                        (
                            [0] => 1964
                        )
                )

            [1] => Array
                (
                    [title] => Array
                        (
                            [0] => Fist Full Of Dollars
                        )

                    [actor] => Array
                        (
                            [0] => Clint Eastwood
                            [1] => Klaus Kinski
                        )

                    [director] => Array
                        (
                            [0] => Sergio Leone
                        )

                    [year] => Array
                        (
                            [0] => 1965
                        )
                )

            [2] => Array
                (
                    [title] => Array
                        (
                            [0] => The Good The Bad and the Ugly
                        )

                    [actor] => Array
                        (
                            [0] => Clint Eastwood
                            [1] => Lee Van Cleef
                            [2] => Eli Wallach
                        )

                    [director] => Array
                        (
                            [0] => Sergio Leone
                        )

                    [year] => Array
                        (
                            [0] => 1966
                        )
                )
        )
)
answer Jan 29, 2016 by Shivaranjini
Similar Questions
0 votes

The output is in this form

Array (
     [0] => name
     [1] => email
     [2] => contact
     [3] => address 
    ) 
Array (
     [0] => sant
     [1] => sant@gmail.com
     [2] => **********
     [3] => haryana ) 

But I want the output in this form

  Array[1] (
     [0] => name
     [1] => email
     [2] => contact
     [3] => address 
    ) 
Array[2] (
     [0] => sant
     [1] => sant@gmail.com
     [2] => **********
     [3] => haryana
    ) 
+2 votes

What are some of the best ways to build XML in C# code?

...