note 55115 added to ref.xml
| From: | pj at netfire dot com dot au | Date: | Tue, 26 Jul 2005 02:55:20 +0000 |
| Subject: | note 55115 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-92441@lists.php.net to get a copy of this message | ||
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");
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55115
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55115&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55115&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55115&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55115&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55115&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55115&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55115&report=yes
Search -- http://master.php.net/manage/user-notes.php