Mr. Print Posted July 16, 2012 Share Posted July 16, 2012 Trying to convert an xml file to array. The below code is my main code which creates a drop-down selection. include ("test_common.php"); ?> <table align="center"> <tr><td> <form action="" name="templatetype" method="post"> <select name="template_name" id="template_name" ><option>Select</option> <?php $array = convertXMLtoArray(); foreach($array as $key=>$value ) { ?> <option value="<?php echo $key;?>" <?php if($_REQUEST['template_name'] == $key) echo "selected"; ?>><?php echo $value;?></option> <?php } ?> </select> </form> </td></tr></table> This is the include file "test_common.php" function convertXMLtoArray() { $template_type = array(); $xml = simplexml_load_file('file2.xml'); foreach($xml->page as $value) { $str = trim($value->attributes()->name); $template_type[$str] = $str; } return $template_type; } This is my xml file, "file2.xml" which works correctly with the above <?xml version="1.0" encoding="utf-8" ?> <cards outerMargin=".0625" outerMarginColor="0xFF0000" innerMarginColor="0x00FFFF"> <page name="Ball Caps" type="Button" w="3" h="3" marginTop=".1875" marginRight="null" marginBottom="null" marginLeft="null" allowCustomSize="true" sizeName="cardSize" allowBackgroundImage="true" allowBackgroundColor="true"> <layer name="Front" /> <layer name="Back" /> </page> <page name="T-Shirt" type="T-Shirts" w="6" h="6" marginTop=".25" marginRight=".25" marginBottom=".25" marginLeft=".25" allowCustomSize="false" allowBackgroundImage="false" allowBackgroundColor="false"> <layer name="Front" shape="ShortFrontSide" /> <layer name="Back" shape="ShortBackSide" /> <layer name="LeftSleeve" w="3" shape="ShortLeftSide" /> <layer name="RightSleeve" w="3" shape="ShortRightSide" /> </page> </cards> However I which for my php code to find the array for the below xml code "file1.xml" and I have tried re-writting the include file every way I can think of, but can't get it to work" <?xml version="1.0" encoding="utf-8"?> <default orientation = "landscape" w = "3.5" h = "2" outerMargin=".125" outerMarginColor="0xFF0000" innerMarginColor="0x00FFFF"/> <pages> <page name = "Ball Caps" w = "3" h = "3" marginTop = ".1875" marginRight = "null" marginBottom = "null" marginLeft = "null"/> <page name = "T-Shirt" w = "2" h = "2" marginTop = ".25" marginRight = ".25" marginBottom = ".25" marginLeft = ".25"/> </pages> Thanks in advance for giving up your time to help. Quote Link to comment https://forums.phpfreaks.com/topic/265772-convert-xml-to-array/ Share on other sites More sharing options...
Barand Posted July 16, 2012 Share Posted July 16, 2012 The second xml is mal-formed - there is no outer element like <cards>...</cards> in the former. If you move "<pages>" before the "<default>" so everything is wrapped in <pages>...</pages> you should have more success. Quote Link to comment https://forums.phpfreaks.com/topic/265772-convert-xml-to-array/#findComment-1361980 Share on other sites More sharing options...
Mr. Print Posted July 17, 2012 Author Share Posted July 17, 2012 Barand, thanks for your reply. Is there a way I can strip or ignore the "<default orientation = "landscape" w = "3.5" h = "2" outerMargin=".125" outerMarginColor="0xFF0000" innerMarginColor="0x00FFFF"/>" and then convert just the area between <pages>? Thanks for your time and reply. Quote Link to comment https://forums.phpfreaks.com/topic/265772-convert-xml-to-array/#findComment-1362024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.