note 25557 added to ref.xml
| From: | guy at bhaktiandvedanta dot com | Date: | Fri, 27 Sep 2002 17:01:08 +0000 |
| Subject: | note 25557 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-37334@lists.php.net to get a copy of this message | ||
For a simple XML parser you can use this function. It doesn't require any extensions to run.
<?
// Extracts content from XML tag
function GetElementByName ($xml, $start, $end) {
global $pos;
$startpos = strpos($xml, $start);
if ($startpos === false) {
return false;
}
$endpos = strpos($xml, $end);
$endpos = $endpos+strlen($end);
$pos = $endpos;
$endpos = $endpos-$startpos;
$endpos = $endpos - strlen($end);
$tag = substr ($xml, $startpos, $endpos);
$tag = substr ($tag, strlen($start));
return $tag;
}
// Open and read xml file. You can replace this with your xml data.
$file = "data.xml";
$pos = 0;
$Nodes = array();
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($getline = fread($fp, 4096)) {
$data = $data . $getline;
}
$count = 0;
$pos = 0;
// Goes throw XML file and creates an array of all <XML_TAG> tags.
while ($node = GetElementByName($data, "<XML_TAG>", "</XML_TAG>")) {
$Nodes[$count] = $node;
$count++;
$data = substr($data, $pos);
}
// Gets infomation from tag siblings.
for ($i=0; $i<$count; $i++) {
$code = GetElementByName($Nodes[$i], "<Code>", "</Code>");
$desc = GetElementByName($Nodes[$i], "<Description>",
"</Description>");
$price = GetElementByName($Nodes[$i], "<BasePrice>",
"</BasePrice>");
}
?>
Hope this helps! :)
Guy Laor
--
http://www.php.net/manual/en/ref.xml.php
http://master.php.net/manage/user-notes.php?action=edit+25557
http://master.php.net/manage/user-notes.php?action=delete+25557
http://master.php.net/manage/user-notes.php?action=reject+25557