我无法使用XMLSerializer修改我的xml .我的代码是
var xml = request.responseText;
var DOMParser = require( 'xmldom' ).DOMParser;
var parser = new DOMParser();
var document = parser.parseFromString( xml, 'text/xml' );
document.getElementsByTagName( "a:Value" ).nodeValue = 12345;
var XMLSerializer = require( 'xmldom' ).XMLSerializer;
var serializer = new XMLSerializer();
var writetofile = serializer.serializeToString( document );
console.log( "writetofile" + writetofile );我得到的XML值与旧值相同,而不是12345。
请让我知道如何解决this..tried所有的options..still不工作。
发布于 2016-11-11 11:04:22
谢谢你再次回复..appricited!
我已经找到了解决方案,希望在这里粘贴它将有助于其他使用textContent属性更改的用户。
document.getElementsByTagName( "a:Value" ).nodeValue = 12345;至
document.getElementsByTagName( "a:Value" )[0].textcontent = 12345;成功了!现在,我将获得整个XML的值。干杯!!
发布于 2016-10-28 15:43:39
尝试将第5行更改为
document.getElementsByTagName("a:Value")[0].childNodes[0].data = '124241';编辑
下面是我在"runkit.com“中编写的代码,以演示如何解决注释中提到的问题。
// some make-up xml
var xml = `
<root>
<a:table xmlns:a="http://www.w3schools.com/furniture">
<a:name>African Coffee Table</a:name>
<a:width>80</a:width>
<a:length>120</a:length>
<a:Value>old value</a:Value>
</a:table>
</root>`;
var DOMParser = require( 'xmldom' ).DOMParser;
var parser = new DOMParser();
var document = parser.parseFromString( xml, 'text/xml' );
// this won't work, but no error
document.getElementsByTagName( "a:Value" ).nodeValue = 12345;
// check: you will get "undefined" on the console
console.log( document.getElementsByTagName("a:Value").nodeValue);
// check: you will get "old value" on the console
console.log( document.getElementsByTagName("a:Value")[0].childNodes[0].data );
// here is another try
document.getElementsByTagName("a:Value")[0].childNodes[0].data = '12345';
// next, you will get "12345" on the console as expected
console.log( document.getElementsByTagName("a:Value")[0].childNodes[0].data );
var XMLSerializer = require( 'xmldom' ).XMLSerializer;
var serializer = new XMLSerializer();
var xmlstring = serializer.serializeToString( document );
console.log( "xmlstring: \n" + xmlstring );Console.log()的相关输出:
undefined
old value
12345
xmlstring:
<root>
<a:table xmlns:a="http://www.w3schools.com/furniture">
<a:name>African Coffee Table</a:name>
<a:width>80</a:width>
<a:length>120</a:length>
<a:Value>12345</a:Value>
</a:table>
</root>https://stackoverflow.com/questions/40302024
复制相似问题