note 55115 deleted from ref.xml by bjori

From: Date: Mon, 09 Oct 2006 11:13:11 +0000
Subject: note 55115 deleted from ref.xml by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-118287@lists.php.net to get a copy of this message
Note Submitter: pj at netfire dot com dot au ---- Here is a very lightweight alternate xml parser object, it comes in handy if installing the xml parsing package is a problem at an installation. Thought this might be useful for someone. <?php ///////////////////////////////////////////////////////////////////// // // Module : SimpleXMLParser.php // DateTime : 26/07/2005 11:32 // Author : Phillip J. Whillier // Purpose : Very lightwieght "simple" XML parser does not support attributes. // ///////////////////////////////////////////////////////////////////// class SimpleXMLParser { // Find the max number of specific nodes in the XML function MaxElements($XMLSource, $XMLName) { $MaxElements = 0; $XMLTag = "<" . $XMLName . ">"; $Y = $this->instr($XMLSource, $XMLTag); while($Y>=0) { $MaxElements = $MaxElements + 1; $Y = $this->instr($XMLSource, $XMLTag, $Y + strlen($XMLTag)); } return $MaxElements; } // Parse xml to retrieve a specific element // Instance number is a zero based index. function Parse($XMLSource, $XMLName, $aInstance = 0, $Default = "") { $XMLLength = strlen($XMLSource); $XMLTag = "<" . $XMLName . ">"; $XMLTagEnd = "</" . $XMLName . ">"; $Instance = $aInstance + 1; /* Find the start of the requested instance... */ $XMLStart = 0; for($x = 1; $x < $Instance + 1; $x++) { $Y = $this->instr($XMLSource, $XMLTag, $XMLStart); if ($Y >= $XMLStart) { $XMLStart = $Y + strlen($XMLTag); } else { return $Default; } } /* Find the end of the instance... */ $XMLEnd = $XMLStart; $XMLMatch = 1; while($XMLMatch) { $c = substr($XMLSource, $XMLEnd, strlen($XMLTagEnd)); if($c == $XMLTagEnd) { $XMLMatch = $XMLMatch - 1; } else { if (substr(c, 0, 1) == $XMLTag) { $XMLMatch = $XMLMatch + 1; } } $XMLEnd = $XMLEnd + 1; if ($XMLEnd == $XMLLength) { return $Dufault; } } return substr($XMLSource, $XMLStart, $XMLEnd - $XMLStart - 1); } // Helper function for finding substrings function instr($haystack, $needle, $pos = 0) { $thispos = strpos($haystack, $needle, $pos); if ($thispos===false) $thispos = -1; return $thispos; } } ///////////////////////////////////////////////////////////////////// // Testing the XML // // TestXML(); function TestXML() { $xmlObj = new SimpleXMLParser; $myxml = "<detail><name>myname</name><name>myname2</name>" . "<name>myname3</name><address><address1>my address</address1>" . "</address></detail>"; $xml = $xmlObj->Parse($myxml, "detail"); print($xml . "\n"); $maxName = $xmlObj->MaxElements($xml, "name"); echo "maxName=$maxName\n"; $name = $xmlObj->Parse($xml, "name", 0); echo "name1=$name\n"; $name = $xmlObj->Parse($xml, "name", 1); echo "name2=$name\n"; $name = $xmlObj->Parse($xml, "name", 2); echo "name3=$name\n"; print("Address:" . $xmlObj->Parse($xmlObj->Parse($xml, "address") , "address1") . "\n"); for($i=0; $i < $maxName; $i++) { $name = $xmlObj->Parse($xml, "name", $i); print("Name:" . $name . "\n"); } } ?>

« previous php.notes (#118287) next »