Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert SimpleXMLObject into PHP Array?

Consider the following code:

$string = '<device>
    <id>1234</id>
    <label>118</label>
    <username>root</username>
    <password>helloWorld</password>
    <hardware>
        <memory>4GB RAM</memory>
        <storage_drives>
            <storage_drive_1>2TB SATA 7,200RPM</storage_drive_1>
            <storage_drive_2>1TB SATA 7,200RPM</storage_drive_2>
            <storage_drive_3>Not Applicable</storage_drive_3>
            <storage_drive_4>Not Applicable</storage_drive_4>
        </storage_drives>
    </hardware>
</device>';
$xml = new SimpleXMLElement($string);

$deviceDetails = Array();
foreach($xml as $element){
    $tag = $element->getName();
    $deviceDetails +=  Array($tag => '$element->$tag)',
        );
    }

Output $detailsDetails array is as follows:

Array
(
    [id] => $element->$tag)
    [label] => $element->$tag)
    [username] => $element->$tag)
    [password] => $element->$tag)
    [hardware] => $element->$tag)
)

which is wrong.

My question is, how to make $element->$tag work?

like image 217
CuriousMind Avatar asked Oct 15 '11 15:10

CuriousMind


People also ask

How do I parse an XML text file into an array?

Step 1: Creating an XML file (Optional): Create an XML file which need to convert into the array. <? xml version = '1.0' ?> Step 2: Convert the file into string: XML file will import into PHP using file_get_contents() function which read the entire file as a string and store into a variable.

How do I create a SimpleXML object?

Example #2 Create a SimpleXMLElement object from a URL$sxe = new SimpleXMLElement('http://example.org/document.xml', NULL, TRUE); echo $sxe->asXML();

What is SimpleXML extension?

SimpleXML is an extension that allows us to easily manipulate and get XML data. SimpleXML provides an easy way of getting an element's name, attributes and textual content if you know the XML document's structure or layout.


2 Answers

Try this:

$string = '<device>
  <id>1234</id>
  <label>118</label>
  <username>root</username>
  <password>helloWorld</password>
  <hardware>
    <memory>4GB RAM</memory>
     <storage_drives>
      <storage_drive_1>2TB SATA 7,200RPM</storage_drive_1>
      <storage_drive_2>1TB SATA 7,200RPM</storage_drive_2>
      <storage_drive_3>Not Applicable</storage_drive_3>
      <storage_drive_4>Not Applicable</storage_drive_4>
    </storage_drives>
  </hardware>
</device>';
  
$xml = json_decode(json_encode((array) simplexml_load_string($string)), 1);

This will output:

Array
(
    [id] => 1234
    [label] => 118
    [username] => root
    [password] => helloWorld
    [hardware] => Array
        (
            [memory] => 4GB RAM
            [storage_drives] => Array
                (
                    [storage_drive_1] => 2TB SATA 7,200RPM
                    [storage_drive_2] => 1TB SATA 7,200RPM
                    [storage_drive_3] => Not Applicable
                    [storage_drive_4] => Not Applicable
                )

        )

)

or if you don't like this, you can use a PHP class like: http://www.bin-co.com/php/scripts/xml2array/

or view dfsq answer

like image 85
Book Of Zeus Avatar answered Oct 17 '22 06:10

Book Of Zeus


Book Of Zeus code wrapped in function to make it work recursively:

function xml2array($xml)
{
    $arr = array();

    foreach ($xml as $element)
    {
        $tag = $element->getName();
        $e = get_object_vars($element);
        if (!empty($e))
        {
            $arr[$tag] = $element instanceof SimpleXMLElement ? xml2array($element) : $e;
        }
        else
        {
            $arr[$tag] = trim($element);
        }
    }

    return $arr;
}

$xml = new SimpleXMLElement($string);
print_r(xml2array($xml));

Array
(
    [id] => 1234
    [label] => 118
    [username] => root
    [password] => helloWorld
    [hardware] => Array
    (
        [memory] => 4GB RAM
        [storage_drives] => Array
        (
            [storage_drive_1] => 2TB SATA 7,200RPM
            [storage_drive_2] => 1TB SATA 7,200RPM
            [storage_drive_3] => Not Applicable
            [storage_drive_4] => Not Applicable
        )
    )
)
like image 33
dfsq Avatar answered Oct 17 '22 06:10

dfsq