PHP XMLParser

PHP XMLParser

PHP XMLParser

PHP XMLParser is the easy way to convert XML file to associative array. Following example converts XML elements to its associative array prints all elements of Array.

(file.xml)

<?xml version="1.0" encoding="UTF-8"?>

<list>
  <item type="Language">
     <name>PHP</name>
     <link>www.php.net</link>
  </item>
  <item type="Database">
    <name>Java</name>
    <link>www.oracle.com/br/technologies/java/‎</link>
  </item>
 </list>
<?php

$xml = simplexml_load_file("file.xml");
 $output = [];
  foreach($xml->children() as $model=>$child) {
    $role = $child->attributes();
      foreach($child as $key => $value) {
         $output[$model][$role.""][$key] = $value."";
      }
  }
 echo "<pre>";
 print_r($output);

?>

Output:

Array
(
    [item] => Array
        (
            [Language] => Array
                (
                    [name] => PHP
                    [link] => www.php.net
                )

            [Database] => Array
                (
                    [name] => Java
                    [link] => www.oracle.com/br/technologies/java/‎
                )

        )

)

Leave a Reply

error: Content is protected !!