Top

Working with SimpleXML


PHP Code

Please Register to submit score.
Bookmark and Share
Average Score  0.0 (of 0 scores)
Date Added  Nov 04, 2009
Last Updated  Nov 04, 2009
Tags  add  loop  php5  simplexml  sort 

Description

Working with simpleXML. 2 functions for looping through XML-files.
formatSortGallery: deletes empty attributes and sort the others
addPrice: adds single attribute to each XML

Grab the Code

<?php
function formatsortGallery($globalDir)
{
	$pfad = $globalDir.'/gallerys/';
	$directory = opendir($pfad);
	while($file = readdir($directory))
	{
		if(filetype($pfad.$file)=='dir' && $file != '.' && $file != '..')
		{	
			$pfad2 = $pfad.$file;
			$directory2 = opendir($pfad2);
			while($file2= readdir($directory2))
			{
				if(preg_match('/.*?gallery\.xml.*?/',$file2,$regs)>0)
				{
					$xml = simplexml_load_file($pfad2.'/'.$file2);				
					$xml2 = simplexml_load_string('<gallery></gallery>');
					foreach($xml->attributes() as $galatt){
						if(trim($galatt)!='')
						{						
							$xml2->addAttribute($galatt->getName(),$galatt);						
						}
					}
					$sortxml = array();
					foreach($xml->image as $images)
					{
						$sortxml[''.trim($images).''] = $images;
					}
					foreach($sortxml as $key=>$find)
					{
						#echo $key."<br/>";
					}				
					ksort(&$sortxml);
					foreach($sortxml as $key=>$find)
					{
						#echo $key."<br/>";
					}
					foreach($sortxml as $image)
					{
 
						$child = $xml2->addChild($image->getName(),$image);
						foreach($image->attributes() as $attr)
						{
 
							if(trim($attr) != '')
							{
								$child->addAttribute($attr->getName(),$attr);
							}
						}
 
 
 
					}
 
					$xml2->asXML($pfad2.'/gallery.xml');
 
 
				}
 
			}
		}
	}
}
 
function addPrice($globalDir)
{
	$pfad = $globalDir.'/gallerys/';
	$directory = opendir($pfad);
	while($file = readdir($directory))
	{
		if(filetype($pfad.$file)=='dir' && $file != '.' && $file != '..')
		{	
			$pfad2 = $pfad.$file;
			$directory2 = opendir($pfad2);
			while($file2= readdir($directory2))
			{
				if(preg_match('/.*?gallery\.xml.*?/',$file2,$regs)>0)
				{
					$xml = simplexml_load_file($pfad2.'/'.$file2);				
 
					$gallery = trim($xml->attributes()->name);
 
					foreach($xml->image as $image)
					{
 
						@$image[0]->addAttribute('price',"1.00");
 
					}
 
					$xml->asXML($pfad2.'/gallery.xml');
 
 
				}
 
			}
		}
	}
}
 
?>
 

Comments

  (0)  RSS

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  

Bottom