You are here:  » Parsing a single node AND the rest trough the loop


Parsing a single node AND the rest trough the loop

Submitted by artemis on Thu, 2011-07-28 02:02 in

Hello,

I'm exporting records from a mysql table to an XML file, and doing so, i'm also adding a single node showing the records count, like this :

<rows>
<records>781</records> (records count)
<row>
<test1>xxx</test1>
<test2>zzz</test2>
</row>
<row>
<test1>yyy</test1>
<test2>zzz</test2>
</row>
</rows>

Then i'm using MagicParser to import the XML (from an another server) into a second Mysql database (It's working like a charm)

My question is how can i also parse the node , wich is actually ouside the loop ?
I need to parse this single node so i can then compare how many records i have in my source database and how many really end in my destination database.

Thank you very much!
PS : David, i love your work, brilliant, plus you're like the king of support :-)

Submitted by support on Thu, 2011-07-28 08:49

Hi artemis,

Thank you for your comments!

I'm going to look at a version of Magic Parser that would let you reference higher level elements e.g. $record["../RECORDS"], but in the mean time, all you need to do (provided that there isn't a memory constraint) is to parse first at the higher level in order to extract the record count, and then again at the ROW level, for example:

  function myRowsRecordHandler($record)
  {
    print "There are ".$record["RECORDS"]." records";
  }
  function myRowRecordHandler($record)
  {
    // handle each row $record as normal here
  }
  MagicParser_parse("data.xml","myRowsRecordHandler","xml|ROWS/");
  MagicParser_parse("data.xml","myRowRecordHandler","xml|ROWS/ROW/");

However if the data is too large to be parsed into a single $record, then in place the first call to MagicParser_parse; use:

  $fp = fopen("data.xml","r");
  $xml = fread($fp,128);
  $xml = substr($xml,strpos("</records>")+10)."</rows>";
  MagicParser_parse("string://".$xml,"myRowsRecordHandler","xml|ROWS/");

Hope this helps!
Cheers,
David.

Submitted by apienczy on Tue, 2012-09-04 00:32

I would like to see similar feature. The solution you presented will work in a lot of cases, but it may not work in my case. The header row should be sunchronized with the rest of the xml. The solution above would call the web service to obtain xml twice at two different times and on the second call the result could be already different. Is there anything else that can be done to not request the xml twice?

Regards,
A

Submitted by support on Tue, 2012-09-04 07:10

Hi,

I do now have an experimental version of Magic Parser that lets you reference parent elements which I will email to you in just a moment. To answer the second part of you question anyway; what you can do (I assume your source is a remote URL rather than local file) is to read the XML into a variable and pass that to Magic Parser using the string:// method, for example:

  $xml = file_get_contents("http://www.example.com/data.xml");
  MagicParser_parse("string://".$xml,"myRowsRecordHandler","xml|ROWS/");
  MagicParser_parse("string://".$xml,"myRowRecordHandler","xml|ROWS/ROW/");

Hope this helps!
Regards,
David
--
MagicParser.com

Submitted by apienczy on Tue, 2012-09-04 13:53

Thanks David,

It looks that it will work. I assume that this method will work with your regular version of Magic Parser and doesn't require the new, experimental version.

On another note I noticed you are using Drupal for your website platform. Have you ever used Magic Parser to load Drupal nodes from an XML feed?

Best,
A

Submitted by support on Tue, 2012-09-04 13:57

Hi,

Yes - the above will work fine with the distribution version! I haven't used Magic Parser within Drupal so far.

Cheers,
David