function xmlStringToArray($xmlString)
{
$xmlString = preg_replace('//s','',$xmlString);
$exitAfterManyLoops = 0;
$xmlArray = array();
$currentNode = &$xmlArray;
$currentHierarchy = array();
$currentDepth = 0;
while($xmlString != '')
{
$exitAfterManyLoops++;
if($exitAfterManyLoops > 300)
{
print "BREAK";
break;
}
$xmlString = trim(substr($xmlString, strpos($xmlString, '<')));
$thisNodeAscends = (substr($xmlString, 1, 1) == '/');
$thisNodeDescends = (substr($xmlString, strpos($xmlString, '>') - 1, 1) != '/');
$nodeName = substr($xmlString, 1, strpos($xmlString, ' ') -1);
$openElement = substr($xmlString, strpos($xmlString, ' ') + 1);
$openElement = substr($openElement, 0, strpos($openElement, '>') );
if(substr($openElement, strlen($openElement) - 1, 1) == "/")
{
$openElement = substr($openElement, 0, strlen($openElement) - 1);
}
if($thisNodeAscends)
{
$currentDepth--;
$currentNode = &$currentHierarchy[$currentDepth];
}
else
{
if($thisNodeDescends)
{
$currentNode[] = array('__attributes' => parseXmlAttributesString($openElement), '__children' => array(), '__nodeName' => $nodeName);
$currentHierarchy[$currentDepth] = &$currentNode;
$currentDepth++;
$lastItem = &$currentNode[count($currentNode) - 1];
$currentNode = &$lastItem['__children'];
}
else //this node is at the same level
{
$currentNode[] = array('__attributes' => parseXmlAttributesString($openElement), '__nodeName' => $nodeName);
}
}
$xmlString = substr($xmlString, strpos($xmlString, '>') + 1);
}
return $xmlArray;
}
function parseXmlAttributesString($xmlElementString)
{
$exitAfter100Loops = 0;
$xmlElementArray = array();
while($xmlElementString != '')
{
$exitAfter100Loops++;
if($exitAfter100Loops > 100)
{
print "BREAK";
break;
}
$equalsCharacterPos = strpos($xmlElementString, '=');
$key = trim(substr($xmlElementString, 0, $equalsCharacterPos));
$xmlElementString = substr($xmlElementString, $equalsCharacterPos + 1);
$openBracket = substr($xmlElementString, 0, 1);
$xmlElementString = substr($xmlElementString, 1);
$endBracketPos = strpos($xmlElementString, $openBracket);
$value = substr($xmlElementString, 0, $endBracketPos);
$xmlElementString = substr($xmlElementString, $endBracketPos + 1);
if($key)
{
$xmlElementArray[$key]=$value;
}
}
return $xmlElementArray;
}
?>
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
相关文章
相关视频
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
312877次学习
收藏310879次学习
收藏366536次学习
收藏