how to insert xml to mysql with php



How to insert XML to Mysql with php? I want to insert data in tag count to xml_id field and data in tag image to image_url field to Mysql . I try this code with xml file it's not work. What am I doing wrong with code. plese tell me.. Here is 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://ift.tt/1yKCF5h;
<image>http://ift.tt/1vhvi5X;
<image>http://ift.tt/1yKCG9g;
<image>http://ift.tt/1vhvi61;
<image>http://ift.tt/1yKCG9k;
<image>http://ift.tt/1vhvi66;
</Images>
</Listing>

<Listing>
<count>2</count>
<Ad_Type>Sale</Ad_Type>
<Unit_Type>Apartment</Unit_Type>
<Images>
<image>http://ift.tt/1yKCF5l;
<image>http://ift.tt/1vhvimq;
<image>http://ift.tt/1yKCI0L;
<image>http://ift.tt/1vhvimy;
</Images>
</Listing>
</Listing>



This is my code.





<?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';
}



No comments:

Post a Comment