xml variable to array

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
habib009pk
Forum Commoner
Posts: 43
Joined: Sun Jul 05, 2009 11:28 pm

xml varialbe to array

Post 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
User avatar
genconv
Forum Commoner
Posts: 34
Joined: Sun Jul 05, 2009 9:27 am

Re: xml variable to array

Post 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
Last edited by genconv on Fri Jul 10, 2009 7:23 am, edited 1 time in total.
User avatar
genconv
Forum Commoner
Posts: 34
Joined: Sun Jul 05, 2009 9:27 am

Re: xml variable to array

Post 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.');
}
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: xml varialbe to array

Post by Christopher »

(#10850)
habib009pk
Forum Commoner
Posts: 43
Joined: Sun Jul 05, 2009 11:28 pm

Re: xml varialbe to array

Post 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
Post Reply