Jump to content

xml to array


vbnullchar

Recommended Posts

i need to convert this xml to an array

 

<?xml version="1.0"?>
<addressBook>
<contact>
<guid>739537250f3146d1968372f982d0fa71</guid>
<email>aborja@abc.com.ph</email>
<fullName>aaaaaaa</fullName>
</contact>
<contact>
<guid>c62a7f3e5d8b48a68cbf0d26999c27f4</guid>
<email>agomez@abc.com.ph</email>
<fullName>bbbbb</fullName>
</contact>
</addressBook>

 

something like this

 

addressbook=array(
contact[0]=array('fullName'=>'aaaaaa', 'guid'=>'739537250f3146d1968372f982d0fa71','email'=>'aborja@abc.com.ph' ),
contact[1]=array('fullName'=>'bbbbbb', 'guid'=>'739537250f3146d1968372f982d0fa71','email'=>'aborja@abc.com.ph' ),
);

Link to comment
Share on other sites

www.php.net/simplexml

 

 

To get it into that type of a form above it would require your own algorithm. Instead of doing that since you are using PHP5, I suggest looking into simplexml. It may solve your problem.

Link to comment
Share on other sites

Already have this done

 

<?php

class xml {
     
     private $url;
     public $data; 
     private $vals;
     private $debug = false;
     
     public function __construct($url) {
         $this->url  = $url;
         $xml = file_get_contents($url);
         $data = $this->xml2array($xml);
         $this->data = $data;
         if ($this->debug) {
             print "<pre>";
             print_r($data);
         }
     }
     
     private function &last(&$array){
        if (!count($array))
            return null;
        end($array);
        return $array[key($array)];
    }
     
     private function parsexml(&$vals, &$dom, &$lev){
        do {
            $curr = current($vals);
            $lev = $curr['level'];
            $tag = strtolower($curr['tag']);
            switch ($curr['type'])
            {
                case 'open':
                    if (!isset($dom[$tag]))
                        $dom[$tag] = array();
                    array_push($dom[$tag], array());
                    $new =& $this->last($dom[$tag]);
                    next($vals);
                    $this->parsexml(&$vals, $new, $lev);
                    break;
            
            case 'cdata':
                break;
            
            case 'complete':
                if (!isset($dom[$tag]))
                    $dom[$tag] = $curr['value'];
                else
                    array_push($dom[$tag] , $curr['value']);
                break;
                
            case 'close':
                return;
            }
        }
        while (next($vals)!==FALSE);
    }

    private function xml2array($xml){
        $xml_parser = xml_parser_create();
        xml_parse_into_struct($xml_parser, $xml, $vals);
        xml_parser_free($xml_parser);
        reset($vals);
        $dom = array();
        $lev = 0;
        $this->parsexml($vals, $dom, $lev);
        return $dom;
    }

}

?>

 

Good luck ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.