note 25557 modified in ref.xml by danbrown

From: Date: Wed, 03 Mar 2010 18:50:00 +0000
Subject: note 25557 modified in ref.xml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166607@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. <?php // 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 --was-- 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://php.net/manual/en/ref.xml.php

« previous php.notes (#166607) next »