smilesmita Posted August 16, 2007 Share Posted August 16, 2007 hi there: i have an xml file which kind of looks like this[as an example i have kept it simple].I want to conver this xml to the array format shown below.dont know how to do it..please help.after i get this array format then it will be easy for me to insert these records into database table by iterating: <xml version.....> <SubscriptionEvents> <subscriptionFile> <filename>filename1</filename> <statustype> <Code> <Date>date1</Date> <Time>time1</Time> </Code> <statustype> <Manifest> <address>address1</address> </Manifest> </subscriptionfile> <subscriptionFile> <filename>filename2</filename> <statustype> <Code> <Date>date2</Date> <Time>time1</Time> </Code> <statustype> <Manifest> <address>address2</address> </Manifest> </subscriptionfile> <SubscriptionEvents> Want an output like this: Array ( [subscriptionEvents]=> [details] => Array ( [0] => Array ( Subscriptionfile=> filename=>filename1 statustype=> Code=> Date=>date1 Time=>time1 Manifest=> address=>address1 ) [1] => Array ( Subscriptionfile=> filename=>filename2 statustype=> Code=> Date=>date2 Time=>time2 Manifest=> address=>address2 ) ) ) EDITED BY akitchin: use code tags, and remember - only you can prevent hard-to-read code. Link to comment https://forums.phpfreaks.com/topic/65290-solved-xml-to-array-format-i-wanthow-to-proceedplease-help/ Share on other sites More sharing options...
nathanmaxsonadil Posted August 16, 2007 Share Posted August 16, 2007 is this what you are looking for? http://www.codewalkers.com/c/a/Database-Code/Improved-XML-To-Array/ BTW: if you had just searched google you could have found it Link to comment https://forums.phpfreaks.com/topic/65290-solved-xml-to-array-format-i-wanthow-to-proceedplease-help/#findComment-326055 Share on other sites More sharing options...
smilesmita Posted August 16, 2007 Author Share Posted August 16, 2007 i tried everything from google.. it doesnt give the output which i want... if you know how to get that array format..that will be great! Link to comment https://forums.phpfreaks.com/topic/65290-solved-xml-to-array-format-i-wanthow-to-proceedplease-help/#findComment-326058 Share on other sites More sharing options...
nathanmaxsonadil Posted August 16, 2007 Share Posted August 16, 2007 there's a link above with a tutorial http://www.codewalkers.com/c/a/Database-Code/Improved-XML-To-Array/ Link to comment https://forums.phpfreaks.com/topic/65290-solved-xml-to-array-format-i-wanthow-to-proceedplease-help/#findComment-326072 Share on other sites More sharing options...
Barand Posted August 16, 2007 Share Posted August 16, 2007 Have you tried simplexml ? <?php $str = '<SubscriptionEvents> <subscriptionFile> <filename>filename1</filename> <statustype> <Code> <Date>date1</Date> <Time>time1</Time> </Code> </statustype> <Manifest> <address>address1</address> </Manifest> </subscriptionFile> <subscriptionFile> <filename>filename2</filename> <statustype> <Code> <Date>date2</Date> <Time>time1</Time> </Code> </statustype> <Manifest> <address>address2</address> </Manifest> </subscriptionFile> </SubscriptionEvents> '; $xml = simplexml_load_string($str); echo '<pre>', print_r($xml, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/65290-solved-xml-to-array-format-i-wanthow-to-proceedplease-help/#findComment-326214 Share on other sites More sharing options...
taith Posted August 16, 2007 Share Posted August 16, 2007 there ya goes... that'll turn any string/file from xml, to a multidimentsional array :-) <?php function xmltoarray(&$string){ $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parse_into_struct($parser, $string, $vals, $index); xml_parser_free($parser); $mnary=array(); $ary=&$mnary; foreach($vals as $r){ $t=$r['tag']; if($r['type']=='open'){ if(isset($ary[$t])){ if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array()); $cv=&$ary[$t][count($ary[$t])-1]; }else $cv=&$ary[$t]; if(isset($r['attributes'])){ foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v; } $cv['_c']=array(); $cv['_c']['_p']=&$ary; $ary=&$cv['_c']; }elseif($r['type']=='complete'){ if(isset($ary[$t])){ if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array()); $cv=&$ary[$t][count($ary[$t])-1]; }else $cv=&$ary[$t]; if (isset($r['attributes'])){ foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v; } $cv['_v']=(isset($r['value']) ? $r['value'] : ''); }elseif($r['type']=='close'){ $ary=&$ary['_p']; } } _del_p($mnary); return $mnary; } function _del_p(&$ary){ foreach($ary as $k=>$v){ if($k==='_p') unset($ary[$k]); elseif(is_array($ary[$k])) _del_p($ary[$k]); } } ?> Link to comment https://forums.phpfreaks.com/topic/65290-solved-xml-to-array-format-i-wanthow-to-proceedplease-help/#findComment-326255 Share on other sites More sharing options...
smilesmita Posted August 17, 2007 Author Share Posted August 17, 2007 unfortunately to use simple array you need php 5 or something.i have php 4.4.4 and it doesnt work on that! Link to comment https://forums.phpfreaks.com/topic/65290-solved-xml-to-array-format-i-wanthow-to-proceedplease-help/#findComment-326593 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.