Page 1 of 1

xml varialbe to array

Posted: Fri Jul 10, 2009 5:37 am
by habib009pk
Dear Friends

I have little problem, i have a variable whose name is $xml which carries all the xml data, now i want to make the array of that data, which parsing function can i use for this process which will directly parse my all data from that xml variable to array or give me any ideal suggestion for this process.

Thanks and Best Regards

Re: xml variable to array

Posted: Fri Jul 10, 2009 6:54 am
by genconv
You can use the simplexml_load_string() function:

Code: Select all

 
$xml_obj = simplexml_load_string($xml);
 
var_dump($xml_obj);
 
It returns an object of class SimpleXMLElement with properties containing the data held within the xml document (within your $xml variable). You can find out more about this function here: PHP Manual simplexml_load_string

Re: xml variable to array

Posted: Fri Jul 10, 2009 7:22 am
by genconv
You can also use the simplexml_load_file() function if you've got an XML file:

Code: Select all

 
if (file_exists('file.xml')) {
    $xml = simplexml_load_file('file.xml'); 
    var_dump($xml);
} else {
    exit('Could not open file.xml.');
}
 

Re: xml varialbe to array

Posted: Fri Jul 10, 2009 10:27 am
by Christopher

Re: xml varialbe to array

Posted: Fri Jul 10, 2009 11:06 pm
by habib009pk
Hi,
I am using your that code

$xml_obj = simplexml_load_string($xml);

var_dump($xml_obj);


but it always return bool(false);
I would like to ask that, can i include any file in my php page as i am working in dreamweaver.

Regards