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;
}
?>
<?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/");
?>
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.