You are here:  » Repeated elements

Support Forum



Repeated elements

Submitted by divulgarte on Sat, 2009-02-28 09:19 in

Hi, I'm trying to parse a Google Adwords XML report, that has this structure:

<?xml version="1.0" standalone="yes"?>
<report>
  <table>
    <columns>
      <column name="campaign"></column>
      <column name="adgroup"></column>
      <column name="agStatus"></column>
      <column name="imps"></column>
      <column name="clicks"></column>
      <column name="ctr"></column>
      <column name="cpc"></column>
      <column name="cost"></column>
      <column name="pos"></column>
      <column name="conv"></column>
      <column name="convRate"></column>
      <column name="costPerConv"></column>
      <column name="transactions"></column>
    </columns>
    <rows>
      <row campaign="CAMPAIGN-1" adgroup="GROUP-1" agStatus="Enabled" imps="1435" clicks="154" ctr="0.1073170731707317" cpc="359610" cost="55380000" pos="1.1540069686" conv="5" convRate="0.032467532467532464" costPerConv="11076000" transactions="10"></row>
      <row campaign="CAMPAIGN-1" adgroup="GROUP-2" agStatus="Enabled" imps="5000" clicks="770" ctr="0.154" cpc="349688" cost="269260000" pos="1.0916" conv="19" convRate="0.024675324675324677" costPerConv="14171578" transactions="21"></row>
      <row campaign="CAMPAIGN-1" adgroup="GROUP-3" agStatus="Enabled" imps="944" clicks="103" ctr="0.10911016949152542" cpc="366213" cost="37720000" pos="1.0762711864" conv="2" convRate="0.019417475728155338" costPerConv="18860000" transactions="3"></row>
    </rows>
  </table>
  <totals>
    <grandtotal imps="7379" clicks="1027" ctr="0.13917875050819895" cpc="352833" cost="362360000" pos="1.1017753083" conv="26" convRate="0.02531645569620253" costPerConv="13936923" transactions="34"></grandtotal>
  </totals>
</report>

I'm not be able to parse more than the FIRST 'ROW', and not the others as there are repeated elements. How can I do that?

Submitted by support on Sun, 2009-03-01 09:02

Hi,

It will be the number of COLUMNS/COLUMN/ fooling the autodetection, whereas you are actually interested in ROWS/ROW. To do this, use the following Format String in the 3rd parameter of your call to MagicParser_parse()...

xml|REPORT/TABLE/ROWS/ROW/

You can see that format string in use with your example data here, and the example source code generated from that demo link is:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
// This is where you write your code to process each record, such as loading a database
    // You can display the record contents using PHP's internal print_r() function:
    
print_r($record);
    
// The following code will print out each field in your sample data:
    
print $record["ROW"];
    print 
$record["ROW-CAMPAIGN"];
    print 
$record["ROW-ADGROUP"];
    print 
$record["ROW-AGSTATUS"];
    print 
$record["ROW-IMPS"];
    print 
$record["ROW-CLICKS"];
    print 
$record["ROW-CTR"];
    print 
$record["ROW-CPC"];
    print 
$record["ROW-COST"];
    print 
$record["ROW-POS"];
    print 
$record["ROW-CONV"];
    print 
$record["ROW-CONVRATE"];
    print 
$record["ROW-COSTPERCONV"];
    print 
$record["ROW-TRANSACTIONS"];
  }
  
$data ""// string variable contianing the data to parse
  
MagicParser_parse("string://".$data,"myRecordHandler","xml|REPORT/TABLE/ROWS/ROW/");
?>

Hope this helps!
Cheers,
David.