Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Posting XML from js to php

    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.

    Cheers,

    Chris

  • #2
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    351
    Thanks
    9
    Thanked 51 Times in 50 Posts
    Interesting.

    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.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    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.
    Last edited by Dormilich; 01-06-2011 at 05:45 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,301
    Thanks
    10
    Thanked 586 Times in 567 Posts
    you have to convert the document to a string, then upload the string.

    see:

    https://developer.mozilla.org/en/Par...erializing_XML
    Code:
    //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;
    }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New Coder
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Ta

    Thank you all. In the end I just constructed it as a string rather than a DOM object, and that posts just fine.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •