在解析和插入节点后获取完整的XML输出

时间:2022-05-17 21:54:06
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>

Code fairly self explanatory.

代码相当自我解释。

I'm retrieving an XML file, appending a new node, and I want to send the output back, but I can't figure out how to get the full output back.

我正在检索一个XML文件,附加一个新节点,我想要发回输出,但我无法弄清楚如何获得完整的输出。

What, seemingly very simple thing, am I missing?

什么,看似很简单的事情,我错过了什么?

1 个解决方案

#1


0  

you can do like this

你可以这样做

$.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();
        }
    }
});

#1


0  

you can do like this

你可以这样做

$.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();
        }
    }
});