Skip to main content

Posts

Showing posts from April, 2011

Parsing XML data and XML file with php

<?php Step1: // Lets parse some xml data $data = '<?xml version="1.0"?> <organization><employees><employee><id>1</id><name>JohnChow</name><age>25</age></employee><employee><id>2</id><name>Simon</name><age>19</age></employee></employees></organization>'; $xml = simplexml_load_string($data);                        $response_arr = array();                                                 foreach ($xml->xpath('//employee') as     $ch) {                            $employee_id = $ch->id;                            $response_arr[(string)$employee_id]=array('id'=>(string)$employee_id,'name'=>(string)$ch->name,'age'=>(string)$ch->age); } print_r($response_arr); Step2: Now lets parse an xml file (with same data as above)   The xml file is shown below as a reference: $fil