PHP XML DOC getElementsByTagName

时间:2017-01-06 06:30:55

标签: php xml getelementsbytagname xml-documentation

因此,如果您将xml文件导入mysql,但xml文件中不存在下面的image_14则会失败,但如果确实存在,则可以正常工作。我会用几个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";
}
?>

1 个答案:

答案 0 :(得分: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";
}
?>

通过这种方式..它会正常工作