1

I am trying to parse a xml file that i have (using javascript DOM). and then i want to insert this data in a mysql table (using php).

I know how to parse xml data. But i am unable to figure out how to use php and javascript together and insert data in mysql table.

Kindly help me in this.

Best Zeeshan

Zeeshan Rang
  • 19,375
  • 28
  • 72
  • 100

3 Answers3

2

The JavaScript needs to send the data to the server. There are several ways this can be achieved.

  1. Generate a form and submit it
  2. Use XMLHttpRequest directly
  3. Use a library like YUI or jQuery

Once there, you get the data (from $_POST for example) and insert it into MySQL as normal.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • this is stupid of me, but can you tell me how should i make a form that has all my xml parsed data, and then which i can submit to php on server so that it can insert each record of my xml in a table – Zeeshan Rang Jun 17 '09 at 15:09
  • var form = document.createElement('form'); form.action='foo.php'; form.method="post"; var data1 = document.createElement('input'); data1.value = myVar; data1.name="data1"; form.appendChild(data1); document.body.appendChild(form); form.submit(); /* Simplified of course */ – Quentin Jun 17 '09 at 17:59
2

Why don't you just use PHP DOM to parse the XML ?

e.g:

$doc = new DOMDocument();
$doc->load('book.xml');
echo $doc->saveXML();

After loaded your document $doc you can use the PHP DOM functions.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Freddy
  • 182
  • 9
  • I would love to use php dom. it will make things so very easy to do right? can you please give me a few sample, where i can learn about how to parse xml in php dom. – Zeeshan Rang Jun 17 '09 at 15:06
1

You can use jQuery to do this, for example:

<house>
 <roof>as</roof>


<door>asd</door><window number="12">iles</window></house>

$('path_to_xml',xml).each(function(){ roof = $('roof',this).text(); door = $('door',this).text(); num_wind = $('window',this).attr('number'); });

red777
  • 354
  • 1
  • 3
  • 11