note 56530 added to ref.xml

From: Date: Tue, 06 Sep 2005 12:36:51 +0000
Subject: note 56530 added to ref.xml
Groups: php.notes 
Request: Send a blank email to php-notes+get-94810@lists.php.net to get a copy of this message
I see there's a few requests for a simple XML parser that doesn't depend on external libraries... this function parses a string of XML, creating nested arrays and attributes. It supports the /> syntax, multiple root nodes, whitespace in key=value attributes, and single/double quote attribute values. It doesn't, however, support text() nodes. <?php function parseXmlString($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) != '/'); $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()); $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)); } } $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; } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.xml.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56530 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56530&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56530&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56530&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56530&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56530&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56530&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56530&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94810) next »