I have an xml file that will be returned to me but will change according to the keyword(s) i send it. I will get a result back, which is marked by a result type and the information and tags will change according to the type that is sent to me. I need to be able to change the format_string or sometimes depending on my result type I'll get a multiple format string's and need to loop and print them differently. So if I set the format strings initially I'll miss being able to loop through the other stings I need?
Here are some dumbed down version of the xml I will get:
<?xml version="1.0" encoding="ISO-8859-1"?>
<result type="1">
<products>
<product>
<title>
Product Title
</title>
<desc>
Product Description
</desc>
</product>
</products>
</result>
<?xml version="1.0" encoding="ISO-8859-1"?>
<result type="2">
<products>
<product>
<title>
Product Title
</title>
<desc>
Product Description
</desc>
</product>
</products>
<store>
<storename></storename>
<price>440</price>
<url>http://www.storeurl.com</url>
</store>
</result>
How would I handle changing the format string? or would I even need to do that? I just want to limit the times I have to call the xml file from the server
Thanks this got me started.
I would like to check the "RESULT-TYPE", like in my example above, and if it is 1 go ahead and parse it using one format string and if it is 2 go ahead and parse it using another format string.
<?php
require("MagicParser.php");
function myRecordHandlerB($record)
{
// first parse handler
print "Title: ".$record['TITLE'];
}
function myRecordHandlerC($record)
{
// second parse handler
print "Store Name: ".$record['STORENAME'];
}
$url = "xml.xml";
$xml = "";
$fp = fopen($url,"r");
while(!feof($fp))
{
$xml .= fread($fp,1024);
}
fclose($fp);
function myRecordHandlerA($record)
{
//Find out what RESULT-TYPE we recieved
$resultType=$record["RESULT-TYPE"];
//Now we'll determine the Parsing type to process
if($resultType==1){
MagicParser_parse("string://".$xml,"myRecordHandlerB","xml/FORMAT/STRING/B");
}
if($resultType==2){
MagicParser_parse("string://".$xml,"myRecordHandlerC","xml/FORMAT/STRING/C");
}
}
MagicParser_parse("string://".$xml,"myRecordHandlerA","xml/FORMAT/STRING/A");
?>
Would this be a way to do it? I was testing this and was able to get it to a certain point but wasn't able to get it to print out either myRecordHandlerB or myRecordHandlerC. Any thoughts on if there is a better way to do it?
Hi,
You're close, but that won't quite work because Magic Parser cannot be called in parallel with an existing call in place. All you need to do is bring the resultType out of myRecordHandlerA within a global variable, and then use it to decide whether to parse with type B or type C. For example:
<?php
require("MagicParser.php");
function myRecordHandlerB($record)
{
// first parse handler
print "Title: ".$record['TITLE'];
}
function myRecordHandlerC($record)
{
// second parse handler
print "Store Name: ".$record['STORENAME'];
}
$url = "xml.xml";
$xml = "";
$fp = fopen($url,"r");
while(!feof($fp))
{
$xml .= fread($fp,1024);
}
fclose($fp);
function myRecordHandlerA($record)
{
global $resultType;
//Find out what RESULT-TYPE we recieved
$resultType=$record["RESULT-TYPE"];
}
MagicParser_parse("string://".$xml,"myRecordHandlerA","xml/FORMAT/STRING/A");
//Now we'll determine the Parsing type to process
if($resultType==1){
MagicParser_parse("string://".$xml,"myRecordHandlerB","xml/FORMAT/STRING/B");
}
if($resultType==2){
MagicParser_parse("string://".$xml,"myRecordHandlerC","xml/FORMAT/STRING/C");
}
?>
Hope this helps,
Cheers,
David.
Hi,
You're right to want to limit the number of times you request the XML - so the best way to handle this (as your server is obviously capable of fopen()'ing a URL), is to read the XML into a local variable, and then use MagicParser's string:// prefix to parse the local data. For example:
<?php
require("MagicParser.php");
function myRecordHandlerA($record)
{
// first parse handler
}
function myRecordHandlerB($record)
{
// second parse handler
}
$url = "http://www.example.com/path/to/file.xml";
$xml = "";
$fp = fopen($url,"r");
while(!feof($fp))
{
$xml .= fread($fp,1024);
}
fclose($fp);
MagicParser_parse("string://".$xml,"myRecordHandlerA","xml/FORMAT/STRING/A");
MagicParser_parse("string://".$xml,"myRecordHandlerB","xml/FORMAT/STRING/B");
?>
Hope this helps!
Cheers,
David.