tags:

views:

104

answers:

2

Hello

I am trying to parse XML using PHP DOM and then insert this data in MySQL tables, i am using the following code for this:

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("testrtap11.xml");
mysql_select_db("zeeshan_database1", $con);

$x=$xmlDoc->getElementsByTagName('RECORD');
$z=$xmlDoc->getElementsByTagName('TITLE');
$w=$xmlDoc->getElementsByTagName('PRIMARY_AUTHOR');
$y=$xmlDoc->getElementsByTagName('JOURNAL_CONFERENCE');

for($i=0; $i<=$x->length-1; $i++)
{
 $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES('$z->item($i)->nodeValue','$w->item($i)->nodeValue','$y->item($i)->nodeValue')";

 if (!mysql_query($sql,$con))
 {
  die('Error: ' . mysql_error());
 }
 echo "1 record added";
}

mysql_close($con)
?>

The data being entered is not correct. i even tried storing my xml parse values in a variable and then using that variable to insert data, even that does not works. I used the variable like this:

for($i=0; $i<=$x->length-1; $i++)
{
 $zz=$z->item($i)->nodeValue);
 $ww=$w->item($i)->nodeValue);
 $yy=$y->item($i)->nodeValue);
 $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES('$zz','$ww','$yy')";

 if (!mysql_query($sql,$con))
 {
  die('Error: ' . mysql_error());
 }
 echo "1 record added";
}

My xml looks like this:

<RTAP>
  <RECORD>
     <TITLE>               </TITLE>
     <PRIMARY_AUTHOR>      </PRIMARY_AUTHOR>
     <JOURNAL_CONFERENCE>  </JOURNAL_CONFERENCE>
  </RECORD>
</RTAP>

Kindly help me, what should I do in this

Best Zeeshan

+2  A: 

Try something like this:

foreach ($xmlDoc->RECORD as $record)
{
    $sql = 'INSERT INTO Persons (FirstName, LastName, Age) VALUES('
     . '"' . mysql_escape_string($record->TITLE->nodeValue) . '", '
     . '"' . mysql_escape_string($record->PRIMARY_AUTHOR->nodeValue) . '", '
     . '"' . mysql_escape_string($record->JOURNAL_CONFERENCE->nodeValue) . '")'

    if (!mysql_query($sql,$con))
     die('Error: ' . mysql_error());

    echo "1 record added";
}

You don't actually say what's wrong with the data that's inserted...

Greg
I tried using the select command:$result = mysql_query("SELECT * FROM Persons"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; }And this is the output i got:(0)->nodeValue (0)->nodeValue(1)->nodeValue (1)->nodeValue(2)->nodeValue (2)->nodeValue(3)->nodeValue (3)->nodeValue
Zeeshan Rang
Greg what is the $record that you have mentioned in your code?is it like $z->item($i)->nodeValue that i am using in mine code. But the thing is your statement is not working for some reason-- i cant not figure out..
Zeeshan Rang
Hey greg thanks, your code did work out after just a few changes in it. thanks alot. Zeeshan
Zeeshan Rang
Oops, I'd made a complete mess of the quotes
Greg
A: 

I tried using the select command on my original code that i mentioned above: $result = mysql_query("SELECT * FROM Persons"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "
"; }

And this is the output i got: (0)->nodeValue (0)->nodeValue (1)->nodeValue (1)->nodeValue (2)->nodeValue (2)->nodeValue (3)->nodeValue (3)->nodeValue

Zeeshan Rang