/    Sign up×
Community /Pin to ProfileBookmark

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; ?>
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@Stephen_PhilbinDec 06.2005 — For help with XML in PHP you'll need to state which version of PHP you're using and if you're using PHP4, which XML library you are using. XML in PHP5 is very different to XML in PHP4 and PHP4 has two possible implementations of XML handling.
×

Success!

Help @cwilkey spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...