1

I want to be able to load an xml file from my server. Edit a node in the xml with jQuery and then save this change with the rest of the xml file back to the server using php. Anyone know how to do this? I have code that changes the xml node and can see this in my console. But cannot get the code back to my server.

Thanks!

<?php
$xml = $_POST['xml'];
$file = fopen("data.xml","w");
fwrite($file, $xml);
fclose($file);
?> 

$.post('saveXml.php', { xml: $(data)}, function(data){alert('data loaded');});

if I console.log(data) I get #document and all the xml nodes. I also get the data.xml file on my server but it's blank.

darylhedley
  • 258
  • 1
  • 8
  • 23

1 Answers1

4

Have never done this before but found some information here: Convert xml to string with jQuery I have tested the following which will modify the original xml and send back to server as a string received by $_POST['xml']

$(function() {
    $.get('test.xml', function(xml) {
        var $xml = $(xml)
         /* change all the author names in original xml*/
        $xml.find('author').each(function() {
            $(this).text('New Author Name');

        })

        var xmlString=jQ_xmlDocToString($xml)

            /* send modified  xml string to server*/    
        $.post('updatexml.php', {xml:xmlString },function (response){             
             console.log(response)
             /* using text dataType to avoid serializing xml returned from `echo $_post['xml'];` in php*/
        }'text')
    }, 'xml')
});

function jQ_xmlDocToString($xml) {
    /* unwrap xml document from jQuery*/
    var doc = $xml[0];
    var string;
    /* for IE*/
    if(window.ActiveXObject) {
        string = doc.xml;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        string = (new XMLSerializer()).serializeToString(doc);
    }
    return string;
}

DEMO: http://jsfiddle.net/54L5g/

Community
  • 1
  • 1
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Does this mean I have to change my xml data to a string before I send it back to the server? @charlietfl – darylhedley Nov 23 '12 at 22:31
  • Could you please have a look at my demo at blinqcreative.co.uk/xml and let me know what I'm missing - my php script is in the example above. Thanks @charlietfl – darylhedley Nov 23 '12 at 22:57
  • you need to convert the xml doc to string as I have in `jQ_xmlDocToString`. You can see in demo that the code I provided works, so try using it – charlietfl Nov 23 '12 at 23:07
  • Ok so used your code and it seems to load and no errors...but...no xml in the new file. Is my php script correct? @charlietfl – darylhedley Nov 23 '12 at 23:18
  • error in console needs to be fixed. If you are going to change the xml it needs to be in `$( data)` so my function expects a jQuery object passed in, not the original doc – charlietfl Nov 23 '12 at 23:21
  • Amazing...I now have xml in a new file! Thanks so much for you patience and help. First time dealing with xml and php - but you've really helped me. – darylhedley Nov 23 '12 at 23:31