You are here:  » Using information from earlier in the XML tree


Using information from earlier in the XML tree

Submitted by Charles Gillespie on Mon, 2006-04-24 19:42 in

Hello - Thanks for any help,

I have tried using the magicparser demo to accomplish what I need the script to do. Alas, it does almost everything.

The XML I am trying to parse with the script is somewhat difficultly formed and hard to parse. Additionally, I am using XML feeds from several companies that all use different formats. I have posted an example of one of these feeds, the Intertops.com XML odds feed, on one of my servers to test with the magicparser demo. See http://www.dragonbets.com/xml/intertops.xml

Magicparser can parse the file successfully with the following format string: xml|ODDS/COMPETITION/MATCH/ . Magicparser, however, does not include information that was located earlier in the XML tree. Specifically, in this feed, matches are organized by sport (2 letter abbreviation), and then again by league (Short string of numbers). I need that information included in each iteration of the magicparser output.

Looking for an answer,

Charles G

Submitted by support on Mon, 2006-04-24 20:19

Hi Charles,

This is possible, but it's not an efficient solution; and so would not be recommended if you are inteneding to parse this XML in real time for display on a website. There may also be issues with memory, because the only way you can do this with Magic Parser involves reading the entire XML tree in as one record using a higher level format string. It works with the demo file you posted (see code below), but if the actual files you are working with contain hunderds of matches, then memory may be an issue.

The trick involves parsing the file twice; once with the top level format string to extract the competition data; and the second parse using the format string as in your original post to extract data about each match.

I've posted a temporary example with your supplied XML - i'll remove this file shortly:

bet.php

Here's the source:

<?php
  
require("MagicParser.php");
  function 
myCompetitionRecordHandler($record)
  {
    global 
$competitionId;
    global 
$competitionSprtyp;
    global 
$competitionCompno;
    
$competitionId $record["COMPETITION-ID"];
    
$competitionSprtyp $record["COMPETITION-SPRTYP"];
    
$competitionCompno $record["COMPETITION-COMPNO"];
  }
  
MagicParser_parse("http://www.dragonbets.com/xml/intertops.xml","myCompetitionRecordHandler","xml|ODDS/COMPETITION/");
  print 
"<h2>Competition ID: ".$competitionId."</h2>";
  print 
"<h2>Competition SPRTYP: ".$competitionSprtyp."</h2>";
  print 
"<h2>Competition COMPNO: ".$competitionCompno."</h2>";
  function 
myMatchRecordHandler($record)
  {
    
// process each match $record here (fields as per demo)
    // if you need to know competition ID etc, just global them in eg:
    
global $competitionId;
    print 
"<h3>".$record["NAME"]." (ID:".$record["MATCH-ID"].")</h3>";
  }
  
MagicParser_parse("http://www.dragonbets.com/xml/intertops.xml","myMatchRecordHandler","xml|ODDS/COMPETITION/MATCH/");
?>

However, there is a second issue with your example XML; in that each match record contains one or more BET elements, and these would have to be extracted with some logic to construct the appropriate $record key values, and is again not straight forward.

I'm sorry that this isn't a complete answer, but to summarise; if you just want to access the summary match data, together with the competition info as per this example; and you are not wanting to do this in real time; then this is probably a viable solution.

If you do want to access the individual BET data then I'm afraid I don't think I could recommend this method of parsing this XML.

Hope this helps!
Regards,
David.

Submitted by Charles Gillespie on Tue, 2006-04-25 04:32

David,

Thanks very much for the prompt and thorough response.

Fortunately, I would be inserting this information into a database, so the performance/memory issues are not a problem.

Unfortunately, as you mentioned toward the end of your response, I do need to process the bet information within each of the MATCH iterations. Without taking too much more of your time, can you suggest a way to process this information?