You are here:  » Update database with hard to get attribute node


Update database with hard to get attribute node

Submitted by bombardos on Sat, 2008-04-12 01:18 in

Hi, having trouble grabbing an node attribute from the xml file to be inserted into a mysql database from the xml file below: (shortened considerably)

<?xml version="1.0" standalone="no" ?>
<!DOCTYPE propertyList>
<propertyList date="2008-04-07-09:15:00" username="_" password="_">
 <residential modTime="2008-04-07-08:40:41" status="current">
  <companyID>1111</companyID>
  <uniqueID>TA080111</uniqueID>
  <authority value="exclusive"/>
  <underoffer value="no"/>
  <listingAgentID>SC</listingAgentID>
  <listingAgentID2/>
  <price display="yes">245000</price>
 </residential>
 <rural modTime="2008-04-07-08:38:10" status="current">
  <companyID>1111</companyID>
  <uniqueID>TA080113</uniqueID>
  <authority value="exclusive"/>
  <underoffer value="no"/>
  <listingAgentID>SC</listingAgentID>
  <listingAgentID2/>
  <price display="yes">339000</price>
 </rural>
 <land modTime="2008-04-07-08:38:37" status="current"
  <companyID>1111</companyID>
  <uniqueID>TA080114</uniqueID>
  <authority value="exclusive"/>
  <underoffer value="no"/>
  <listingAgentID>SC</listingAgentID>
  <listingAgentID2/>
  <price display="yes">249000</price>
 </land>
 <residential modTime="2008-04-07-08:45:22" status="current">
  <companyID>1111</companyID>
  <uniqueID>TEA080304</uniqueID>
  <authority value="exclusive"/>
  <underoffer value="no"/>
  <listingAgentID>AJC</listingAgentID>
  <listingAgentID2/>
  <price display="yes">320000</price>
 </residential>
</propertyList>

I need the attribute because sometimes we will recieve an xml file where the properties are already in the database and ony the status of the house has changed.
I can get all the other info into the database without a problem with the following:

<?php
if ($foo)
{
    
$dir opendir ($dirOld);
    while (
false !== ($file readdir($dir))) {
        if (
strpos($file'.xml',1)) {
            
$fileToLoad $file;
        }
    }
    
$filename $dirOld .'/'$fileToLoad;
    
$format_string MagicParser_getFormat($filename);
    if (!
$format_string)
    {
        print 
'<p>'.MagicParser_getErrorMessage().'</p>';
        exit;
    }
    else
    {
        echo 
'';
    }
    
$counter 0;
    echo 
'<p>Inserting new records from the xml file:</p>';
    function 
myRecordHandler($record)
    {
        global 
$counter;
        global 
$sql;
        
$counter++;
        global 
$dbServer$dbUser$dbPass$dbName;
        
$cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName);
        
$check 'SELECT uniqueID FROM listings WHERE uniqueID = "'$record["UNIQUEID"] .'"';
        
$result_ck mysql_query($check) or die (mysql_error() ."Couldn't execute check query");
        
$row_ck mysql_fetch_array($result_ck);
        if (
$record["UNIQUEID"] != $row_ck["uniqueID"])
        {
            
$sql "INSERT INTO listings (companyID,uniqueID,..etc..."
            
$sql .= " VALUES ('".$record["COMPANYID"]."','".$record["UNIQUEID"]."',...etc...";
            
$result mysql_query($sql) or die (mysql_error() ."Couldn't execute insert query");
        }
        else
        {
            echo 
$record["UNIQUEID"] .' already in database<br>';
        }
    }
    
MagicParser_parse($filename,"myRecordHandler","xml|PROPERTYLIST/RESIDENTIAL/");
    
MagicParser_parse($filename,"myRecordHandler","xml|PROPERTYLIST/RURAL/");
    
MagicParser_parse($filename,"myRecordHandler","xml|PROPERTYLIST/LAND/");
    print 
"<p>Processed ".$counter." records.<br>";
    print 
"Insert complete...</p>";
    print 
mysql_error();
    
//exit;
}
?>

but I also need the status from PROPERTYLIST/RESIDENTIAL-STATUS, PROPERTYLIST/RURAL-STATUS, AND PROPERTYLIST/LAND-STATUS which has got me stumped.
I initially tried combining a variation of the script below with the script above to get the status with not much luck, so then tried repeating the process using an sql update using various combinations, the following code is my last combination attempt:
<?php
function myUpdateHandler($updater)
{
    global 
$counter;
    global 
$sql;
    
$counter++;
    global 
$dbServer$dbUser$dbPass$dbName;
    
$cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName);
    
$check 'SELECT status, uniqueID FROM listings WHERE uniqueID = "'$updater["RESIDENTIAL/UNIQUEID"] .'" OR uniqueID = "'$updater["RURAL/UNIQUEID"] .'" OR uniqueID = "'$updater["LAND/UNIQUEID"] .'"';
    
$result_ck mysql_query($check) or die (mysql_error() ."Couldn't execute second check query");
    
$row_ck mysql_fetch_array($result_ck);
    if ((
$updater["RESIDENTIAL/UNIQUEID"] == $row['uniqueID']) || ($updater["RURAL/UNIQUEID"] == $row['uniqueID']) || ($updater["LAND/UNIQUEID"] == $row['uniqueID']))
    {
        
$spath MagicParser_parse($filename,"myUpdateHandler","xml|PROPERTYLIST/");
        switch(
$spath)
        {
            case 
$updater['RESIDENTIAL-STATUS']:
                
$status $updater['RESIDENTIAL-STATUS'];
                break;
            case 
$updater['RURAL-STATUS']:
                
$status $updater['RURAL-STATUS'];
                break;
            case 
$updater['LAND-STATUS']:
                
$status $updater['LAND-STATUS'];
                break;
        }
        
$sql "UPDATE listings SET status = '"$status ."'";
        
$result mysql_query($sql) or die (mysql_error() ."Couldn't execute update query");
    }
}
MagicParser_parse($filename,"myUpdateHandler","xml|PROPERTYLIST/");
?>

Can the great minds help me with this one please?

Submitted by support on Sat, 2008-04-12 08:33

Hi,

The status attribute appears in $record as either:

$record["RESIDENTIAL-STATUS"]
$record["LAND-STATUS"]
or
$record["RURAL-STATUS"]

I notice from your code that you are handling all cases identically, so I suspect this is where you are
not sure how to access this field.

As each record is short, a neat way to access the status attribute whatever it is actually called would
be to do this:

foreach($record as $k => $v)
{
  if (strpos($k,"STATUS")) { $status = $v; break; }
}

Simply insert this at the beginning of your myRecordHandler() function, and then use the $status variable
to determine whether you need to INSERT or UPDATE.

Hope this help!
Cheers,
David.

Submitted by bombardos on Mon, 2008-06-16 23:54

Yes it did help, thankyou very much David.