note 26542 deleted from ref.xml by danbrown
| From: | danbrown@php.net | Date: | Wed, 03 Mar 2010 18:49:51 +0000 |
| Subject: | note 26542 deleted from ref.xml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166606@lists.php.net to get a copy of this message | ||
Note Submitter: sfaulkner at hoovers dot com
----
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);