note 26542 added to ref.xml
| From: | sfaulkner at hoovers dot com | Date: | Mon, 04 Nov 2002 06:29:48 +0000 |
| Subject: | note 26542 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-38940@lists.php.net to get a copy of this message | ||
Building on... This allows you to return the value of an element using an XPath reference. This
code would of course need error handling added :-)
function GetElementByName ($xml, $start, $end) {
$startpos = strpos($xml, $start);
if ($startpos === false) {
return false;
}
$endpos = strpos($xml, $end);
$endpos = $endpos+strlen($end);
$endpos = $endpos-$startpos;
$endpos = $endpos - strlen($end);
$tag = substr ($xml, $startpos, $endpos);
$tag = substr ($tag, strlen($start));
return $tag;
}
function XPathValue($XPath,$XML) {
$XPathArray = explode("/",$XPath);
$node = $XML;
while (list($key,$value) = each($XPathArray)) {
$node = GetElementByName($node, "<$value>", "</$value>");
}
return $node;
}
print XPathValue("Response/Shipment/TotalCharges/Value",$xml);
--
http://www.php.net/manual/en/ref.xml.php
http://master.php.net/manage/user-notes.php?action=edit+26542
http://master.php.net/manage/user-notes.php?action=delete+26542
http://master.php.net/manage/user-notes.php?action=reject+26542