如何用php将xml插入mysql

时间:2014-11-24 07:32:54

标签: php mysql xml

如何使用php将XML插入Mysql?

我想将标记计数中的数据插入到xml_id字段,将标记图像中的数据插入到image_url字段到Mysql。我用xml文件尝试了这段代码,但没有用。我在做什么错误的代码。请告诉我。

这是XML。

<?xml version="1.0" encoding="utf-8"?>
<Listing>
	<Listing>
	<count>1</count>
	<Ad_Type>Sale</Ad_Type>
	<Unit_Type>Apartment</Unit_Type>
	<Images>
		<image>http://example.com/watermark?c_id=1110&l_id=1908007&aid=1461&image=e296915d71.jpg</image>
		<image>http://example.com/watermark?c_id=1110&l_id=1908007&aid=1461&image=acf49f1088.jpg</image>
		<image>http://example.com/watermark?c_id=1110&l_id=1908007&aid=1461&image=10a639c856.jpg</image>
		<image>http://example.com/watermark?c_id=1110&l_id=1908007&aid=1461&image=9f9db4243e.jpg</image>
		<image>http://example.com/watermark?c_id=1110&l_id=1908007&aid=1461&image=v127aca728.jpg</image>
		<image>http://example.com/watermark?c_id=1110&l_id=1908007&aid=1461&image=g19bdac010.jpg</image>
	</Images>
	</Listing>

	<Listing>
	<count>2</count>
	<Ad_Type>Sale</Ad_Type>
	<Unit_Type>Apartment</Unit_Type>
	<Images>
		<image>http://example.com/watermark?c_id=1110&l_id=1907998&aid=1461&image=vv96915d71.jpg</image>
		<image>http://example.com/watermark?c_id=1110&l_id=1907998&aid=1461&image=g19bdac010.jpg</image>
		<image>http://example.com/watermark?c_id=1110&l_id=1907998&aid=1461&image=8127aca728.jpg</image>
		<image>http://example.com/watermark?c_id=1110&l_id=1907998&aid=1461&image=9f9db4243e.jpg</image>
	</Images>
	</Listing>
</Listing>

这是我的代码。

<?php
include("connection.php");

$xmlUrl = "import_dubai.xml"; // XML ไฟล์   
$xmlStr = file_get_contents($xmlUrl);  
$xml = preg_replace('#&(?=[a-z_0-9]+=)#', '&amp;', $xmlStr);

foreach($xml -> Listing as $Listing1){       // Listing => array

   foreach($Listing1 -> Listing as $Listing){    // Listing => array

	   $Listing = $Listing -> Listing;
	   $count = $Listing -> count;				

       foreach($Listing -> Images as $Images){   // Images => array

		  $image = $Images -> image;
		  echo '<br>'.$Listing1 -> Listing.' '.$Listing -> count.' '.$Listing -> Images.' '.$Images -> image. '<br>';
		  $result=mysql_query("insert into xml_image ('xml_id','image_url') values ('$count','$image')");
		}
							
  	}
}

if (!$result){
	echo 'Mysql Error';
} else{
    echo ' Success';	
}

?>

2 个答案:

答案 0 :(得分:6)

我为所提供的xml文件引入了一个错误处理程序,以便您可以获得有关xml内容实际发生或更新错误的更详细信息。请查看

<?php
include("connection.php")

$xmlUrl = "import_dubai.xml"; // XML ไฟล์
list ($xml, $error) = parseFile($xmlUrl);
if ($xml === FALSE) {
  throw new Exception("Failed to parse your XML file: $error");
}

foreach($xml -> Listing as $Listing1){       // Listing => array
foreach($Listing1 -> Listing as $Listing){    // Listing => array
   $Listing = $Listing -> Listing;
   $count = $Listing -> count;              
   foreach($Listing -> Images as $Images){   // Images => array
      $image = $Images -> image;
      echo '<br>'.$Listing1 -> Listing.' '.$Listing -> count.' '.$Listing -> Images.' '.$Images -> image. '<br>';
      $result=mysql_query("insert into xml_image ('xml_id','image_url') values ('$count','$image')");
    }

  }
}

if (!$result){
  echo 'Mysql Error';
} else{
  echo ' Success';  
}

 function parseFile($file) {
  $xml = FALSE; // SimpleXMLElement
  $error = FALSE; // string

  if (! file_exists($file)) {
    $error = 'File ' . $file . ' does not exist.';
  } else {
     $oldLibXMLErrors = libxml_use_internal_errors();
     libxml_use_internal_errors(TRUE);

     $xml = simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA);
     if ($xml === FALSE) {
       $errors = libxml_get_errors();
       foreach ($errors as $error) {
         if ($error->level != LIBXML_ERR_ERROR && $error->level != LIBXML_ERR_FATAL) {
           continue;
       }

       $parts = array();
       if ($error->file) {
       $parts[] = "File=$error->file";
     }
     $parts[] = "Line=$error->line";
     $parts[] = "Column=$error->column";
     $parts[] = "Code=$error->code";

     $messages[] = implode(" ", $parts) . ": " . trim($error->message);
   }
   $error = implode("\n", $messages);
  }

  libxml_use_internal_errors($oldLibXMLErrors);
  }

  return array($xml, $error);
}

答案 1 :(得分:0)

这一行:

$xml = preg_replace('#&(?=[a-z_0-9]+=)#', '&amp;', $xmlStr);

返回一个字符串,而不是使用

遍历的对象
foreach ($xml->Listing as $Listing1) ...

如果你想使用XML,那么你需要额外的一步:

$formatted = preg_replace('#&(?=[a-z_0-9]+=)#', '&amp;', $xmlStr);
$xml = simplexml_load_string($formatted);

这会带你走向正确的方向。