All pastes #171466 Raw Edit

Stuff

public php v1 · immutable
·published 2006-09-15 12:49 UTC
rendered paste body
// i'm using this function to parse xml into an array// thing is that some xml i'm receiving contains a / in the attributes like here  <orderheader customer_ordernumber="IO06/004621-1158314630" order_number="0010554587" sequencenumber="1" />// thereby the attributes after this / are not parsed...function xml2array ($xml){	$xmlary = array ();	if ((strlen ($xml) < 256) && is_file ($xml))	$xml = file_get_contents ($xml);	$ReElements = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*?)<(\/\s*\1\s*)>)/s';	$ReAttributes = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';	preg_match_all ($ReElements, $xml, $elements);	foreach ($elements[1] as $ie => $xx) {		$xmlary[$ie]["name"] = $elements[1][$ie];		if ( $attributes = trim($elements[2][$ie])) {			preg_match_all ($ReAttributes, $attributes, $att);			foreach ($att[1] as $ia => $xx)			// all the attributes for current element are added here			$xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];		} // if $attributes		// get text if it's combined with sub elements		$cdend = strpos($elements[3][$ie],"<");		if ($cdend > 0) {			$xmlary[$ie]["text"] = substr($elements[3][$ie],0,$cdend -1);		} // if cdend		if (preg_match ($ReElements, $elements[3][$ie])){			$xmlary[$ie]["elements"] = xml2array ($elements[3][$ie]);		}		else if (isset($elements[3][$ie])){			$xmlary[$ie]["text"] = $elements[3][$ie];		}		$xmlary[$ie]["closetag"] = $elements[4][$ie];	}//foreach ?	return $xmlary;}