Jump to content

Convert XML to array


Mr. Print

Recommended Posts

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/265772-convert-xml-to-array/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.