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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

2 Comments

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
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 */
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'); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.