• Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us
Blue Orange Green Pink Purple

Convert XML to Array in PHP

Posted in PHP/MySQL. on Friday, January 29th, 2010 by Aneeska Tags: array, convert xml to array, domdocument, php, simplexml, xml, xml to array, xml2array
Jan 29

Convert XML to Array in PHP usingĀ  SimpleXML

Though I would suggest using DomDocument for parsing XML strings and files in PHP, here is an example of converting XML to an Array in PHP using SimpleXML method. The SimpleXML extension in PHP provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.

The code I have posted below traverse through all the child nodes of the XML up to the deepest level and forms an array in the same structure. It takes care of the CDATA tags as well, that you get the text defined inside the CDATA tag. One thing this code doesn’t do is parsing the attributes. This code will avoid the attributes in a tag. You can just easily add this feature yourself by using the “attributes” method of SimpleXML.

<?php
$xmlData = "<FileInfo>
 <File>
 <Name id='4'><![CDATA[Test.txt]]></Name>
 <MD5>c63f02d0fee180d1b0e1f49cb52edba3</MD5>
 <LastModified>2008-09-05T14:42:44.000 01:00</LastModified>
 <Path><![CDATA[/Users/haat/Desktop/Test folder/carriage_rets.txt]]></Path>
 <Size>181</Size>
 </File>
 <File>
 <Name id='4'>My.jpg</Name>
 <MD5>c63f02d0fee180d1b0e1f49cb52edba3</MD5>
 <LastModified>2008-09-05T14:42:44.000 01:00</LastModified>
 <Path><![CDATA[/Users/haat/Desktop/Test folder/carriage_rets.txt]]></Path>
 <Size>181</Size>
 </File>
</FileInfo>";
function xmlToArray($input, $callback = null, $recurse = false) {
 $data = ((!$recurse) && is_string($input))? simplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA): $input;
 if ($data instanceof SimpleXMLElement) $data = (array) $data;
 if (is_array($data)) foreach ($data as &$item) $item = xmlToArray($item, $callback, true);
 return (!is_array($data) && is_callable($callback))? call_user_func($callback, $data): $data;
}
$fileinfo_array = xmlToArray($xmlData);
echo "<pre>";
print_r($fileinfo_array);
echo "</pre>";
?>

And this is the output you would get:

Array
(
    [File] => Array
        (
            [0] => Array
                (
                    [Name] => Test.txt
                    [MD5] => c63f02d0fee180d1b0e1f49cb52edba3
                    [LastModified] => 2008-09-05T14:42:44.000 01:00
                    [Path] => /Users/haat/Desktop/Test folder/carriage_rets.txt
                    [Size] => 181
                )

            [1] => Array
                (
                    [Name] => My.jpg
                    [MD5] => c63f02d0fee180d1b0e1f49cb52edba3
                    [LastModified] => 2008-09-05T14:42:44.000 01:00
                    [Path] => /Users/haat/Desktop/Test folder/carriage_rets.txt
                    [Size] => 181
                )

        )

)

The above code uses “simplexml_load_string” to load an XML string to parse. If you need to load an XML file and parse, you could use “simplexml_load_file”.

Just run the PHP code and play around. Ask me if you have any questions.


ASK a Question!

Bookmark and Share

2 Comments

  1. palmeida on April 27th, 2010

    Thank you, works like a charm!

  2. Aneeska on May 3rd, 2010

    Great to know. You are welcome.



Leave a Reply

CAPTCHA Image CAPTCHA Audio
Refresh Image
  • Search a Topic


  • Got a Question?
      ASK and get a Solution! ASK
  • Categories
    • CakePHP
    • Computers
    • CSS & XHTML
    • Enterprise Web
    • Flash & Action Script
    • Flex
    • Javascript & Libraries
    • Perl Programming
    • PHP/MySQL
    • Programming Techniques
    • Regular Expression
    • Social Media
  • Recent Visitors
  • Recent Articles
    • How to Protect your SWF (AS2 or AS3). Prevent SWF Decompile using SWF Protector
    • PHP – Search in an Array for Values Matching a Pattern – Regex, Wildcard
    • PHP – How to Get Browser Properties and Capabilities – get_browser()
    • CakePHP – Search for records between two dates inclusively
    • Notebook/Laptop hangs frequently/random (on Low Battery when AC power is connected)
    • Swap Values Without Temp or A Third Variable in PHP
    • CakePHP – Auth Login Redirect Problem with Plugins
    • Convert PHP array to Javascript array
    • CakePHP – Accessing a model in AppController or in any Controllers
    • Save Images from Flash – Actionscript 3, Filereference.save, JPGEncoder
  • Archives
    • September 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
  • Tags
      vertically center horizontally align dead center div amoeba ask credit card workbench decryption solutions kiosk questions horizontal center align vertical center align css trick solution css encryption as2 flash regex as3 Regular Expression action script mysql array CakePHP php

  • Donate

      If you find this site helpful, please donate to Amoeba Solution Kiosk. We will be using the money received for conducting technology awareness camps and seminars for school and college students in rural areas of Kerala, India.

  • Links
    • Amoeba Solutions
    • Blog Catalog
  • Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us

© Copyright Amoeba Solution Kiosk. All rights reserved.
An Amoeba Concept!

Back to Top