Kapil Singhal
Kapil Singhal

Reputation: 17

XML data insertion into MySql DB

i am trying to store a XML file into MySQL DB. Here is my XML File-

<items>

 <item>

  <title>Google</title>

  <url>google.com</url>

 </item>

 <item>

  <title>Google Accounts</title>

  <url>accounts.google.com</url>

 </item>

</items> 

and here is my php file-

<?php
$xmlDoc = new DOMDocument();

$xmlDoc->load("items.xml");

$mysql_hostname = "localhost";

$mysql_user = "digifizz_voice";

$mysql_password = "admin";

$mysql_database = "digifizz_voicemail";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)

or die("Opps some thing went wrong");

mysql_select_db($mysql_database, $bd)

or die("Opps some thing went wrong");

$x=$xmlDoc->getElementsByTagName('item');

for ($i=0; $i<=15000; $i++)

  {

  $title=$x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;

  $link=$x->item($i)->getElementsByTagName('url')->item(0)->childNodes->item(0)->nodeValue;

 $insert = "INSERT INTO items (title, url) VALUES ('$title', '$link')";

 $add_member = mysql_query($insert);

}

?>

But when i run this php on my web server i am gettinh this error-

Fatal error: Call to a member function getElementsByTagName() on a non-object in /home/xxxx/public_html/xxx/add.php on line 28

How can i resolve this error.

Upvotes: 0

Views: 53

Answers (1)

Avikarsha Saha
Avikarsha Saha

Reputation: 133

You need to some thing like

$dom->loadXML(file_get_contents("items.xml"));

Upvotes: 1

Related Questions