PHP XML DOC getElementsByTagName

Поэтому, если вы импортируете xml-файл в mysql, но изображение image_14, приведенное ниже, не существует в xml-файле, это приведет к сбою, но если оно существует, это сработает. Я буду делать это с несколькими файлами XML, некоторые могут иметь все 14, а некоторые нет.

<data>
<Images>
<item0>
<ImageID>1</ImageID>
</item0>
<item1>
<ImageID>2</ImageID>
</item1>
</data><?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("file.xml");
$mysql_hostname = ""; // Example : localhost
$mysql_user     = "";
$mysql_password = "";
$mysql_database = "";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");

$xmlObject = $xmlDoc->getElementsByTagName('data');
$itemCount = $xmlObject->length;

for ($i=0; $i < $itemCount; $i++){
$image_1  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(0)->childNodes->item(0)->nodeValue;
$image_2  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(1)->childNodes->item(0)->nodeValue;
$image_3  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(2)->childNodes->item(0)->nodeValue;
$image_4  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(3)->childNodes->item(0)->nodeValue;
$image_5  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(4)->childNodes->item(0)->nodeValue;
$image_6  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(5)->childNodes->item(0)->nodeValue;
$image_7  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(6)->childNodes->item(0)->nodeValue;
$image_8  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(7)->childNodes->item(0)->nodeValue;
$image_9  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(8)->childNodes->item(0)->nodeValue;
$image_10  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(9)->childNodes->item(0)->nodeValue;
$image_11  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(10)->childNodes->item(0)->nodeValue;
$image_12  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(11)->childNodes->item(0)->nodeValue;
$image_13  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(12)->childNodes->item(0)->nodeValue;
$image_14  = $xmlObject->item($i)->getElementsByTagName('ImageID')->item(13)->childNodes->item(0)->nodeValue;
$sql   = "INSERT INTO `$site` (
image_1,
image_2,
image_3,
image_4,
image_5,
image_6,
image_7,
image_8,
image_9,
image_10,
image_11,
image_12,
image_13,
image_14,
image_15,
image_16,
image_17,
image_18,
image_19,
image_20,
image_21,
image_22,
image_23,
image_24,
image_25,
image_26,
image_27,
image_28,
image_29,
image_30
) VALUES ('$image_1', '$image_2', '$image_3', '$image_4', '$image_5', '$image_6', '$image_7', '$image_8', '$image_9', '$image_10', '$image_11', '$image_12', '$image_13', '$image_14', '$image_15', '$image_16', '$image_17', '$image_18', '$image_19', '$image_20', '$image_21', '$image_22', '$image_23', '$image_24', '$image_25', '$image_26', '$image_27', '$image_28', '$image_29', '$image_30')";
mysql_query($sql);
print "Finished";
}
?>

0

Решение

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("file.xml");
$mysql_hostname = ""; // Example : localhost
$mysql_user     = "";
$mysql_password = "";
$mysql_database = "";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");

$xmlObject = $xmlDoc->getElementsByTagName('data');
$itemCount = $xmlObject->length;

for ($i=0; $i < $itemCount; $i++){
$image = $xmlObject->item($i)->getElementsByTagName('ImageID');
$total_items_in_ImageID_tag = $image->length;
$total_images = [];
for($j = 0 ; $j < $total_items_in_ImageID_tag; $j++){
$total_images["image_".$j] = $image->item($j)->childNodes->item(0)->nodeValue;
}
$insert_keys_array = array_keys($total_images);
$insert_key_string = implode(",", $insert_keys_array);

$data_to_be_inserted = "";
foreach ($total_images as $image_value) {
$data_to_be_inserted.= "'".$image_value."',";
}

$sql_query = "INSERT INTO `$site` ( ". $insert_key_string.") VALUES (". rtrim($data_to_be_inserted, ",").")";
/**
* Query excution
*/
mysql_query($sql_query);
print "Finished";
}
?>

Таким образом .. это будет работать нормально

0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]