View Full Version : Posting XML from js to php

01-05-2011, 10:26 PM
Hello all

I have written a web page to create XML from user input. The user inputs a series of values, and these are turned into XML using javascript. The way I have done this is to load in a template XML file using an XMLHTTPrequest and append childNodes to it as required. Thus, what I end up with is a variable called "xmldoc" which is an XML document object. (If I do write(xmldoc) the answer I get is [object XMLDocument].)

What I would like to do is to be able to save the thing as an xml file. To this end I have been trying to POST it to a php file that might do this, but to no avail; the php file does not seem to recognise it as an XML DOM object. I just can't work out how I might do this, and all suggestions would be gratefully received.



01-06-2011, 02:08 PM

Perhaps you should do this at interactive XML node level,
instead of complete XML document object, using ajax:

Client's XMLDocument object <-- AJAX --> Server's PHP DOMDocument class.

01-06-2011, 06:38 PM
unfortunately, AJAX does not send XML*, it can only retrieve XML.

* you can of course send (serialised) XML as value part of a key-value pair.

rnd me
01-06-2011, 06:40 PM
you have to convert the document to a string, then upload the string.



//stolen from http://www.sencha.com/forum/showthread.php?34553-Convert-DOM-XML-Document-to-string
function XMLtoString(elem){

var serialized;

try {
// XMLSerializer exists in current Mozilla browsers
serializer = new XMLSerializer();
serialized = serializer.serializeToString(elem);
catch (e) {
// Internet Explorer has a different approach to serializing XML
serialized = elem.xml;

return serialized;

01-11-2011, 12:55 PM
Thank you all. In the end I just constructed it as a string rather than a DOM object, and that posts just fine.