如何更新已解析的xml值

时间:2019-04-23 21:15:56

标签: javascript html ajax xml dom

我正在将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;

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

1 个答案:

答案 0 :(得分: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);