Could someone take a look at this and let me know what I’m missing? If you do an “echo $data;” you will see the correct tree structure. I’m trying to exstract a specific childe node, “temperature”.
[code=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 = “http://wp.myweather.net/wxdata/current.asp?pub=mywx123456&s=kuin”;
$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, “<reporting-station>”, “</reporting-station>”)) {
$Nodes[$count] = $node;
$count++;
$data = substr($data, $pos);
}
// Gets infomation from tag siblings.
for ($i=0; $i<$count; $i++) {
$code = GetElementByName($Nodes[$i], “<temperature>”, “</temperature>”);
}
?>
<?php echo $code; ?>