以前从未这样做过,但在这里找到了一些信息: Convert xml to string with jQuery
我已经测试了以下内容,它将修改原始 xml 并作为接收到的字符串发送回服务器$_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;
}
演示:http: //jsfiddle.net/54L5g/