簡體   English   中英

使用jquery加載xml文件,編輯它,然后用php再次保存到服務器

[英]Use jquery to load xml file, edit it, then save to server again with php

我希望能夠從我的服務器加載一個xml文件。 使用jQuery編輯xml中的節點,然后使用php將此更改與xml文件的其余部分一起保存回服務器。 有人知道怎么做嗎? 我有更改xml節點的代碼,可以在我的控制台中看到它。 但無法將代碼返回給我的服務器。

謝謝!

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

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

如果我是console.log(數據)我得到#document和所有xml節點。 我也在我的服務器上獲取data.xml文件,但它是空白的。

從來沒有這樣做過但在這里找到了一些信息: 用jQuery將xml轉換為字符串我已經測試了以下內容,它將修改原始的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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM