我一整天都在寻找,并找到了一些有希望的选择,但这些选择并没有为我提供(即Is it possible to add,delete xml nodes with jquery $.ajax?)。我相信我最大的问题不是对AJAX或XML过于熟悉,而是简化了问题: 我需要通过HTML页面更新本地XML页面(不使用PHP或其他服务器端工具)。我在供应商提供的解决方案中工作(因此我无法对产品进行太多修改)。
以下是我工作的片段: mobileSettings.xml:
<settings>
<application>
<firstCase>PublicMonitor</firstCase>
</application>
</settings>
customSettings.js:
function loadSettings()
{
$.ajax({
type: "GET",
url: '../configurations/mobileSettings.xml',
dataType: "xml",
success: function(xml) {
$(xml).find('firstCase').each(function () {
startPage.value = $(this).text();
});
}
});
}
我无法工作的东西也是同一个.js的一部分,但它适用于Save。您可以通过注释代码看到许多失败的尝试。 (在加载和保存之间,用户可以更改一个简单的下拉框以选择新值。):
function saveSettings()
{
startP = startPage.value;
$.ajax({
type: "PUT",
url: '../configurations/mobileSettings.xml',
dataType: "xml",
success: function(xml) {
$(xml).find('firstCase').each(function () {
// $(this).text(startP);
$(this).attr(startP);
// $(this).find('firstCase').text("test");
// $(this).setAttribute("firstCase", "test");
// var elem = document.createElement('firstCase');
// xml.find('application').append(elem); // Appends <app></app> to <layout>
// $(this).find('firstCase').text() = startPage.value;
});
}
});
}
我的假设是这里没有任何东西可以将数据发送到XML。我一直在不知疲倦地寻找,但我找不到真正有效的解决方案。同样,我不能使用服务器端脚本,因此解决方案必须是AJAX / JQuery / Javascript ...我不知道它会起作用。
提前致谢。
编辑07.02.2015
我相信我可能不会像过去那样清楚。我的问题在于通过AJAX等更改.xml文件。我知道如何操作用户页面上的数据,但是我需要用户能够声明一个需要保存到现有.xml的新值。页。用户有一个下拉菜单,它将为他们提供有效值,在提交时,XML文件需要将firstCase节点更改为其他节点:
<settings>
<application>
<firstCase>PrivateMonitor</firstCase>
</application>
</settings>
评论中提供的小提琴(http://jsfiddle.net/py5dvk6e/)适用于创建xml字符串,但我不知道如何将该字符串保存到customSettings.js文件中调用的.xml文件中(上图)。