如何更新已解析的xml值

问题描述 投票:0回答:1

我正在解析XML作为对象并通过标记名访问节点,我有一个问题,我希望看到更新的值不会更新。警报显示了值,它是正确的。但我需要它显示在文档上,而不是它。

var x = xml.responseXML;
var v1 = document.getElementById("sid");
alert(x.getElementsByTagName("ID")[0].childNodes[0].nodeValue);
v1.innerText = x.getElementsByTagName("ID")[0].childNodes[0].nodeValue;

我还有一个问题,如何允许编辑/突出显示节点?

javascript html ajax xml dom
1个回答
0
投票

如果nodeValuenull,设置它的值没有效果(来自the docs)。但是,您可以通过其他方式修改XML内容,例如.innerHTML.innerText.value等。 .innerHTML的示例:

// creates a Document, as in XMLHttpRequest.responseXML
const docText = `
<!DOCTYPE html>
<body>
  <div>Hello</div>
  <div>World</div>
</body>`;
const doc = (new DOMParser()).parseFromString(docText, 'application/xml');

// setting nodeValue over null has no effect...
console.log(doc.getElementsByTagName('div')[0].nodeValue);
doc.getElementsByTagName('div')[0].nodeValue = 'Bye';
console.log(doc.getElementsByTagName('div')[0].nodeValue);

// ...but you can modifies the XML in different ways
console.log(doc.getElementsByTagName('div')[0].innerHTML);
doc.getElementsByTagName('div')[0].innerHTML = 'Bye';
console.log(doc.getElementsByTagName('div')[0].innerHTML);
© www.soinside.com 2019 - 2024. All rights reserved.