Hello PHP Builders!
I'm new to the forum, I've used the forum to answer a lot of my PHP questions in the past and it's been very helpful.
I'm having a small problem with PHP that I'm hoping someone can help me solve:
I have a script that can parse XML when reading from a file and store each part of the XML in a variable, it works great:
<?
// 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://hello.com/thefile.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, "<ServicePlanData>", "</ServicePlanData>")) {
$Nodes[$count] = $node;
$count++;
$data = substr($data, $pos);
}
// Gets infomation from tag siblings.
for ($i=0; $i<$count; $i++) {
$Minutes_Used = GetElementByName($Nodes[$i], "<UsedLogins>", "</UsedLogins>");
$Start_Date = GetElementByName($Nodes[$i], "<StartDate>", "</StartDate>");
$Exp_Date = GetElementByName($Nodes[$i], "<ExpirationDate>", "</ExpirationDate>");
$Plan_Status = GetElementByName($Nodes[$i], "<PlanStatus>", "</PlanStatus>");
$ESN_LIVE = GetElementByName($Nodes[$i], "<Number>", "</Number>");
}
?>
My problem is, I have XML thats not coming from a external file and I need to know how to parse it. I have a script that post XML to a web service then returns the result ($result). Can anyone tell me how to parse this since it's not on an external page like the script above? Any help would be appreciated.