XML into MySQL table



I have the following XML in a file called "items.xml" in 'c:\'. I have setup WAMP and I have this php to put xml content into MySQL table "test.items". But it doesn't work. Please help.



<?xml version="1.0" encoding="windows-1252" standalone="yes"?>
<items>
<item>
<title>Google</title>
<url>google.com</url>
<des>google.com</des>
</item>
<item>
<title>Google Accounts</title>
<url>accounts.google.com</url>
<des>google.com</des>
</item>
</items>


PHP code is;



<?php

$servername = "localhost";
$username = "root";
$password = "";

$conn = mysqli_connect($servername, $username, $password);

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
else{echo "Connected";


$oDOM = new DOMDocument();
$oDOM->loadXML(file_get_contents('c:\items.xml'));
foreach ($oDOM->getElementsByTagName('item') as $Conf);

$raw1 = mysql_real_escape_string($Conf->getElementsByTagName('title')->item(0)->nodeValue);
$raw2 = mysql_real_escape_string($Conf->getElementsByTagName('url')->item(0)->nodeValue);
$raw3 = mysql_real_escape_string($Conf->getElementsByTagName('des')->item(0)->nodeValue);

$sql = "insert into `test`.`items` (`title`, `url`, `des`) values ('$raw1', '$raw2', '$raw3')";
$insert = mysqli_query($conn, $sql);

}

No comments:

Post a Comment