All pastes #386478 Raw Edit

Stuff

public php v1 · immutable
·published 2007-03-08 17:23 UTC
rendered paste body
<?php// Groundup's XML Parser/*We want to get a structure like:tag_name = array(	attributes => array(attribute1 => value, attribute2 => value),	value => ''	children => array (		tag_name = array(			attributes => array()			value => 'some value'		)	))Don't worry about namespace support for now.You may ask what about type='complete'. Well, we don't need to worry about it.	Ideas:		Implement pagination.*/error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);define('VALUE_LIMIT', 255);$file = './packman/Themes/packages1.xml';//$file = 'http://mods.simplemachines.org/packages.xml';$parser = xml_parser_create();xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);xml_parse_into_struct($parser, file_get_contents($file), $vals, $index);xml_parser_free($parser);$sizeofXML = count($vals);$counter = 0;echo '<pre>';$result = xmlToArray($vals, count($vals));print_r($result);echo '</pre>';// Turns XML files in to a multi-level array.function xmlToArray ($xml, $limit = false, $start = 0){	global $counter, $sizeofXML;		//$counter = !empty($counter) ? $counter : $start;	$this_level = 0;		$array = array();	$count = $limit === false ? $sizeofXML : $limit - $start;	$i = $counter;	for (; $i < $count; $i++)	{//echo $counter, "\n";		++$counter;//echo $counter, "\n";		// Reference to make future code cleaner.		$element = &$xml[$counter];		// Skip cdata. It doesn't do anything for us here.		if ($element['type'] == 'cdata')			continue;		// End the node. (I know it seems backwards to end it before starting it. Otherwise you get an extra row in there)		if ($element['type'] == 'close')		{//echo $i; print_r($element);echo $counter, ' ', $this_level, "\n";			//$counter += $this_level;			return $array;		}		// Set up the node.		if (!isset($array[$this_level]))			$array[$this_level] = array('tag' => $element['tag']);		$tag = &$array[$this_level];		// DEBUGGING!		$tag['xml_array_num'] = $counter;		$tag['xml_array_limit'] = $count - $counter;		// Add the attributes.		if (isset($element['attributes']))			$tag['attributes'] = $element['attributes'];		// Maybe this element has a value? Lets add it.		if (!empty($element['value']))							$tag['value'] = VALUE_LIMIT > 0 ? substr($element['value'], 0, VALUE_LIMIT) : $element['value'];		// Let's open it up for children (child nodes)... recursive.		if ($element['type'] == 'open')		{// echo $i, ' ', $i+1, "\n";			//$counter++;			$tag['children'] = xmlToArray($xml, $count - $i);		}		// A counter to track the array.		$this_level++;		//$counter++;	}	return $array;}?>