You are here:  » Matching and extracting a special informations ( Tags + attributes )

Support Forum



Matching and extracting a special informations ( Tags + attributes )

Submitted by lucasmd on Tue, 2008-01-01 14:50 in

Hi,

I d like to create RecordHandler function where I will be able to find all banners (element tag with attributs for width and size and mime type) having a width of 250px associated to a particular "title" tag. There is a variable quantity of elements in each "prog" tags.

Can you help me because I am really lost and I do not know how to do that ?

Here is a sample of my xml feed :

{code saved}

Submitted by support on Wed, 2008-01-02 12:10

Hi,

The trick for dealing with this kind of XML layout (to be honest not ideally suited to Magic Parser) is to loop through every element in $record using foreach(), looking for tags that end in "-WIDTH". When one is found, check the value for the ones you are interested in (250 in this example), and then set a flag that says the next elements with the values you are interested should be used (so we set $use = TRUE). Then, you do the same thing to pick out the fields you want.

I have written an example to do this, picking out all TAGS/IMG/ELEMENT-URL values, but it can be easily modified to extract other values at the same level. Here's the code:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
$title $record["TITLE"];
    print 
"<h2>".$title."</h2>";
    foreach(
$record as $k => $v)
    {
      if (
strpos($k,"-WIDTH"))
      {
        if (
$v == "250")
        {
          
$use TRUE;
        }
        else
        {
          
$use FALSE;
        }
      }
      if (
strpos($k,"-URL") && $use)
      {
        
$url $v;
        print 
"<p>250px Width Banner URL: ".$url."</p>";
      }
    }
  }
  
MagicParser_parse("listing.xml","myRecordHandler","xml|LISTING/PROG/");
?>

Click here to see the output running on this server.

At the point in this example where the banner URL is displayed, you can use $url and $title as required - I have only printed the value for example purposes.

Hope this helps!
Cheers,
David.

Submitted by lucasmd on Wed, 2008-01-02 12:36

Thanks for your function but unfortunatelly the URL attributes provide only the picture but noit the tracking code... :-) It is my fault because I did not explained it to you very well before...

I have started to build the following function but the last problem I have is that I am not able to grab the "@number" of the TRACK tag following the ELEMENT tag.

Do you have an idea for correcting my function ?

  function myRecordHandler($record)
  {
global $csv;
$row = "";
$m = 0;
while(1) {
if ($m) $postfixm = "@".$m;
if (!isset($record["TAGS/IMG/ELEMENT".$postfixm])) break;
$i = 0;
while(1) {
if ($i) $postfix = "@".$i;
if (!isset($record["TAGS/IMG/ELEMENT/TRACK".$postfix])) break;
if($record["TAGS/IMG/ELEMENT".$postfix."-WIDTH"]=="250" && $record["TAGS/IMG/ELEMENT".$postfix."-HEIGHT"]=="250") {
// $z is false because I didn't found the logical rule for calculating it
$z = "@".$i*2;
if($record["TAGS/IMG/ELEMENT/TRACK".$z."-SEC"]=="0") {
$row .= $record["PROG-ID"]."\t".$record["TITLE"]."\t".$record["TAGS/IMG/ELEMENT/TRACK".$z]."\t".$record["TAGS/IMG/ELEMENT".$postfix."-WIDTH"]."\t".$record["TAGS/IMG/ELEMENT".$postfix."-HEIGHT"]."\n";
}
}
$i++;
}
$m++;
}
    fwrite($csv,$row);
}

Submitted by support on Wed, 2008-01-02 12:48

Hi,

The example can be changed to extract the track element. I would suggest inspecting the value as the XML format is not conducive to analysing in series i'm afraid. Here's an example:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    print 
"<h2>".$record["TITLE"]."</h2>";
    foreacH(
$record as $k => $v)
    {
      if (
strpos($k,"-WIDTH"))
      {
        if (
$v == "250")
        {
          
$use TRUE;
        }
        else
        {
          
$use FALSE;
        }
      }
      if ((
substr($v,0,2) == "<a") && $use)
      {
        
$track $v;
        print 
"<p>".htmlentities($track)."</p>";
      }
    }
  }
  
MagicParser_parse("listing.xml","myRecordHandler","xml|LISTING/PROG/");
?>

Cheers,
David.

Submitted by lucasmd on Sat, 2008-01-05 20:09

Thanks a lot, it is working with a terrific efficiency :-)

I am using that on : http://www.dealgates.co.uk and http://www.dealgates.fr