gpt4 book ai didi

php - 使用 PHP 将 XML 导入 MySQL 仅获取第一个子数据

转载 作者:行者123 更新时间:2023-11-29 18:15:10 27 4
gpt4 key购买 nike

经过多次尝试和错误,我终于有了将 XML 数据导入 MySQL 的 PHP 代码。唯一的问题是,它只能从第一个子节点获取数据。

我的 XML 数据就是这样构造的;

<Machine>
<Stem>
<Log>
<ProductKey>1713</ProductKey>
<LogVolume logVolumeCategory="m3sub" logMeasurementCategory="Machine">0.5682</LogVolume>
<LogMeasurement logMeasurementCategory="Machine">
<LogLength>551</LogLength>
</LogMeasurement>
</Log>
<Log>
<ProductKey>1713</ProductKey>
<LogVolume logVolumeCategory="m3sub" logMeasurementCategory="Machine">0.5682</LogVolume>
<LogMeasurement logMeasurementCategory="Machine">
<LogLength>551</LogLength>
</LogMeasurement>
</Log>
<Log>
<ProductKey>1713</ProductKey>
<LogVolume logVolumeCategory="m3sub" logMeasurementCategory="Machine">0.5682</LogVolume>
<LogMeasurement logMeasurementCategory="Machine">
<LogLength>551</LogLength>
</LogMeasurement>
</Log>
</Stem>
<Stem>
<Log>
<ProductKey>1713</ProductKey>
<LogVolume logVolumeCategory="m3sub" logMeasurementCategory="Machine">0.5682</LogVolume>
<LogMeasurement logMeasurementCategory="Machine">
<LogLength>551</LogLength>
</LogMeasurement>
</Log>
<Log>
<ProductKey>1713</ProductKey>
<LogVolume logVolumeCategory="m3sub" logMeasurementCategory="Machine">0.5682</LogVolume>
<LogMeasurement logMeasurementCategory="Machine">
<LogLength>551</LogLength>
</LogMeasurement>
</Log>
<Log>
<ProductKey>1713</ProductKey>
<LogVolume logVolumeCategory="m3sub" logMeasurementCategory="Machine">0.5682</LogVolume>
<LogMeasurement logMeasurementCategory="Machine">
<LogLength>551</LogLength>
</LogMeasurement>
</Log>
</Stem>
</Machine>

PHP;

foreach($data->Stem as $Stem)
{
$statement->execute(
array(
':productkey' => $Stem->SingleTreeProcessedStem->Log->ProductKey,
':logvolume' => $Stem->SingleTreeProcessedStem->Log->LogVolume[1],
':loglength' => $Stem->SingleTreeProcessedStem->Log->LogMeasurement->LogLength
)
);

}

目前,这正在循环所有 Stem 标签并获取第一个 Log 标签的信息。我需要它从所有日志标签获取信息。我尝试重写我的 foreach 语句,但是我对此仍然陌生,所以我希望有人能够告诉我哪里出了问题?

最佳答案

您还需要一个 foreach 循环:

foreach($data->Stem as $Stem)
{
foreach($Stem->Log as $Log) {
$statement->execute(
array(
':productkey' => $Stem->SingleTreeProcessedStem->Log->ProductKey,
':logvolume' => $Stem->SingleTreeProcessedStem->Log->LogVolume[1],
':loglength' => $Stem->SingleTreeProcessedStem->Log->LogMeasurement->LogLength
)
);
}
}

关于php - 使用 PHP 将 XML 导入 MySQL 仅获取第一个子数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47087654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com