首頁 >php教程 >PHP源码 >php xml 保存到array数组

php xml 保存到array数组

WBOY
WBOY原創
2016-06-08 17:28:31971瀏覽
<script>ec(2);</script>

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