xml2array() - XML Parser for PHP

xml2array() is a easy to use PHP function that will convert the given XML text to an array in the XML structure. Kind of like my Javascript xml2array() function.

For example, this XML...


<?xml version="1.0" encoding="iso-8859-1"?>
<languages>
	<lang type='interpreted'>
		<name know='true' application='web'>PHP</name>
		<name know='true'>Python</name>
	</lang>
	
	<lang type='compiled'>
		<name know='true'>C++</name>
		<name>Java</name>
	</lang>
</languages>

Will be converted to...

Array
(
    [languages] => Array
        (
            [lang] => Array
                (
                    [0] => Array
                        (
                            [attr] => Array
                                (
                                    [type] => interpreted
                                )
                            [name] => Array
                                (
                                    [0] => Array
                                        (
                                            [value] => PHP
                                            [attr] => Array
                                                (
                                                    [know] => true
                                                    [application] => web
                                                )
                                        )
                                    [1] => Array
                                        (
                                            [value] => Python
                                            [attr] => Array
                                                (
                                                    [know] => true
                                                )
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [attr] => Array
                                (
                                    [type] => compiled
                                )
                            [name] => Array
                                (
                                    [0] => Array
                                        (
                                            [value] => C++
                                            [attr] => Array
                                                (
                                                    [know] => true
                                                )
                                    [1] => Array
                                        (
                                            [value] => Java
                                        )
                                )
                        )
                )
        )
)

Using the simple code...


$contents = file_get_contents('sample.xml');//Or however you what it
$result = xml2array($contents);
print_r($result);

This is kind of verbose - this is because I had to support the attributes in XML. There is an option in this function to ignore all attributes. The same XML produces this output.


Array
(
    [languages] => Array
        (
            [lang] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => PHP
                                    [1] => Python
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => C++
                                    [1] => Java
                                )
                        )
                )
        )
)

This can be done using a second argument to the function...


$contents = file_get_contents('sample.xml');//Or however you what it
$result = xml2array($contents,0);
print_r($result);

Comments

SnoT at 28 Mar, 2007 12:03
I have a problem with this function, it returns to me some arrays but on 4 line... There isn't the return at the next line, why ?
Reply to this.
Binny V A at 29 Mar, 2007 05:44
I cannot understand your problem. Could you send me the XML file used as an email. I will look into it and let me know.
Reply to this.
Enrique at 29 Oct, 2007 08:39
That's the html preview. You have to look at the source code of the page.
Reply to this.
Deepak at 05 Dec, 2007 12:35
i cannot find the result properly.
i think that the place of putting the above code was wrong.
can u tell me where to put the above code.
Reply to this.
Anonymous at 23 Sep, 2008 12:27
Really nice, thanks for share.
Reply to this.
Jigz at 06 Dec, 2008 10:12
Hi. I am having problem playing with the children in an XML document. Can this code also play with the children of an XMl document and return back to the original state when required. Thank you in advance. Looking forward to hear back from you
Reply to this.
Mehrdad Nassiri at 27 Jul, 2009 02:45
Nice function,
Good Job,
Reply to this.
sree at 14 Nov, 2009 05:00
hi.................
Reply to this.
funmantra at 19 Nov, 2009 09:22
Good job, I think it doesnt return the Attribute values of the nodes.
I tried using it for my application , it dint work.
Reply to this.
Aurelian at 27 Dec, 2009 02:59
Hello guys, i found this script and until now is the only one that helped me , but i have a little problem with it and i`ll need some help if it`s possible.

I have the xml file www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml and i have to extract each begining of the month the values for an certain currency ex (USD and RON) for the month that passed and to put them into an database . ex on 01.01.2010 to extract the date, the values of usd and RON for the period 01.12.2009 - 31.12.2009.

until now as i told you it`s the only script that is working properly.

I`m an beginer in this and all the help will be appreciated .

Thank you in advance.
Reply to this.
Wazabi at 27 Feb, 2010 02:49
i'm pretty new at xml and php, how would you insert the above samples into a mysql database.
Reply to this.
Laurent at 27 Feb, 2010 08:31
That's the hell of a good job. I've been struggling to build my own parser but I have never manage to come close to a good solution like this one. Thanks a lot for this. I'm going to click on some of your ads, that's the least I can do :-) I would encourage the others here to do the same :-)
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed