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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.