areuin
新手上路

UID 69390
精华
0
积分 14
帖子 60
金钱 14 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2006-3-13
状态 离线
|
[推荐阅读] 10元求解决个问题
哈哈,我也是等了好久了.我们老大写了一个,但是还不是很满意.
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;
}
|
|