var fileUrl = '/files/file.xml';
$.ajax({
type: "GET",
url: fileUrl,
dataType: "xml",
success: parseResultsXML
});
function parseResultsXML(xml) {
var dom = $(xml);
var item = $.parseXML("<SubNode><somevalue>new value</somevalue></SubNode>");
$(dom).children(0).prepend($(item).children(0));
//Error
console.log(dom.html());
//See Output below
console.log(dom.children().html());
}
//Original Xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SubNode>
<somevalue>value</somevalue>
</SubNode>
</RootNode>
//Output
<SubNode>
<somevalue>new value</somevalue>
</SubNode>
<SubNode>
<somevalue>value</somevalue>
</SubNode>
//Expected Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SubNode>
<somevalue>new value</somevalue>
</SubNode>
<SubNode>
<somevalue>value</somevalue>
</SubNode>
</RootNode>
代码相当自我解释。
我正在检索XML文件,附加一个新节点,我想要将输出发回,但我无法弄清楚如何获得完整的输出。
什么,看似很简单的事情,我错过了吗?
答案 0 :(得分:0)
你可以这样做
$.ajax({
type: 'GET',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
url: fileUrl,
dataType: "xml",
parseResultsXML: function(xml) {
if (xml.readyState == 4 && xml.status == 200) {
var somevalue = $(xml.SubNode).find('somevalue').text();
}
}
});