我目前正在使用' simplexml_load_file'从XML文件中读取。我正在寻找多个值并将它们存储在一些参数中,我稍后会在SQL插入语句中使用它们。
我可以确认没有返回任何错误。此外,在执行我的SQl语句之前,我已经打印了这些值,以确保那里没有出错,并且echo没有打印出任何内容。我想可能在获取值时语法可能不正确,但我不确定。下面是我的PHP:
$xml = simplexml_load_file("{$path}/{$email_safe}/{$datafile}.xml");
// Retreive data details for specified activity
$totalTime = $xml->TrainingCenterDatabase->Activities->Activity->Lap->TotalTimeSeconds;
$distance = $xml->TrainingCenterDatabase->Activities->Activity->Lap->DistanceMeters;
$maxSpeed = $xml->TrainingCenterDatabase->Activities->Activity->Lap->MaximumSpeed;
$calories = $xml->TrainingCenterDatabase->Activities->Activity->Lap->Calories;
$intensity = $xml->TrainingCenterDatabase->Activities->Activity->Lap->Intensity;
$trigMethod = $xml->TrainingCenterDatabase->Activities->Activity->Lap->TriggerMethod;
// Store activity details into the 'detail' table
$sqlDetail = "INSERT INTO detail (detailID,TotalTime,distance,maxSpeed,calories,intensity,trigMethod) VALUES (\"$datafile\",\"$totalTime\",\"$distance\",\"$maxSpeed\",\"$calories\",\"$intensity\",\"$trigMethod\")";
$runDetail = mysql_query($sqlDetail) or die("unable to complete INSERT action:$sql:".mysql_error());
以下是我试图阅读的XML文件片段:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/ActivityExtension/v2 http://www.garmin.com/xmlschemas/ActivityExtensionv2.xsd http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">
<Activities>
<Activity Sport="Running">
<Id>2014-02-17T20:23:52Z</Id>
<Lap StartTime="2014-02-17T20:23:52Z">
<TotalTimeSeconds>1585.4799805</TotalTimeSeconds>
<DistanceMeters>113.5199966</DistanceMeters>
<MaximumSpeed>1.3720000</MaximumSpeed>
<Calories>3</Calories>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
如果有人能够在这里指出我的错误,我将不胜感激。感谢。
答案 0 :(得分:0)
查看您提供的示例XML,似乎“TrainingCenterDatabase”不是树的一部分 -
改变这个:
$xml = simplexml_load_file("{$path}/{$email_safe}/{$datafile}.xml");
// Retreive data details for specified activity
$totalTime = $xml->TrainingCenterDatabase->Activities->Activity->Lap->TotalTimeSeconds;
$distance = $xml->TrainingCenterDatabase->Activities->Activity->Lap->DistanceMeters;
$maxSpeed = $xml->TrainingCenterDatabase->Activities->Activity->Lap->MaximumSpeed;
$calories = $xml->TrainingCenterDatabase->Activities->Activity->Lap->Calories;
$intensity = $xml->TrainingCenterDatabase->Activities->Activity->Lap->Intensity;
$trigMethod = $xml->TrainingCenterDatabase->Activities->Activity->Lap->TriggerMethod;
对此:
$xml = simplexml_load_file("{$path}/{$email_safe}/{$datafile}.xml");
// Retreive data details for specified activity
$totalTime = $xml->Activities->Activity->Lap->TotalTimeSeconds;
$distance = $xml->Activities->Activity->Lap->DistanceMeters;
$maxSpeed = $xml->Activities->Activity->Lap->MaximumSpeed;
$calories = $xml->Activities->Activity->Lap->Calories;
$intensity = $xml->Activities->Activity->Lap->Intensity;
$trigMethod = $xml->Activities->Activity->Lap->TriggerMethod;