gpt4 book ai didi

php - SimpleXML 解析多个同名标签

转载 作者:行者123 更新时间:2023-11-30 00:08:56 26 4
gpt4 key购买 nike

请,我需要解析包含汽车信息的 XML 文件。我在汽车、照片中完成了所有操作,但我需要在额外的表中包含 id、汽车表 id 的外键,并且所有汽车都有随机的 Equipment_id。

我需要将其放入“设备”表中:

  <equipment>
<equipment_id>eq_co_driver_airbag</equipment_id>
<equipment_id>eq_driver_airbag</equipment_id>
<equipment_id>eq_abs</equipment_id>
<equipment_id>eq_aut_aircon</equipment_id>
<equipment_id>eq_dalk_centr</equipment_id>
<equipment_id>eq_el_contr_win</equipment_id>
<equipment_id>eq_el_contr_mirrors</equipment_id>
<equipment_id>eq_lite_disky</equipment_id>
<equipment_id>eq_immobilizer</equipment_id>
<equipment_id>eq_mlhovky</equipment_id>
<equipment_id>eq_adjustable_drive_wheel</equipment_id>
<equipment_id>eq_original_autoradio</equipment_id>
<equipment_id>eq_on_board_comp</equipment_id>
<equipment_id>eq_steering_booster</equipment_id>
<equipment_id>eq_leather_trim</equipment_id>
<equipment_id>eq_ASR</equipment_id>
<equipment_id>eq_stresni_sibr_el</equipment_id>
<equipment_id>eq_ton_skla</equipment_id>
<equipment_id>eq_venkovni_teplomer</equipment_id>
<equipment_id>eq_adjustable_seat</equipment_id>
</equipment>

我无法创建正确的导入脚本。

这是我的 XML 文件中的一辆车: http://pastebin.com/Yyr1GXyb

这是通过 print_r 列出的一辆车的列表: http://pastebin.com/csQpQSDs

我尝试过这个:

echo "import equipment .. ";
foreach ($xml->cars->car->equipment as $data) {
mysql_query("INSERT INTO equipment (cars_id, equipment_id)
VALUES
($id_car, \"$data->equipment_id\")")
or die(mysql_error());
$id_car++;
}
echo "OK\n";

还有这个

echo "import equipment .. ";
foreach ($xml->cars->car as $entry) {
foreach ($entry->equipment as $data) {
mysql_query("INSERT INTO equipment (cars_id, equipment_id)
VALUES
($id_car, \"$data->equipment_id\")")
or die(mysql_error());
}
$id_car++;
}
echo "OK\n";

但没有成功

我想不通。请帮我。非常感谢!

最佳答案

解决了!

echo "import equipment .. ";
foreach ($xml->cars->car as $entry) {
foreach ($entry->children()->equipment->equipment_id as $data) {
mysql_query("INSERT INTO equipment (cars_id, equipment_id)
VALUES
($id_car, \"$data\")")
or die(mysql_error());
}
$id_car++;
}
echo "OK\n";

关于php - SimpleXML 解析多个同名标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24248804/

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