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

php xml 保存到array数组

WBOY
Lepaskan: 2016-06-08 17:28:31
asal
971 orang telah melayarinya
<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;
        }

?>

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan