xmldom
xmldom copied to clipboard
Serialize fails to account for text node updates
Assuming D is a parsed-in XML DOM. Assume further some text nodes in D are modified using D.getElementsByTagName(...)[0].childNodes[0].nodeValue = ...;
Serializing D produces the original (unmodified parsed-in DOM)! However, explicit reference to the modified nodes produces the expected (modified) values!
Issue demo code...
var winston = require('winston'); //multi-transport logger
var logger = new (winston.Logger) ({ transports: [ new winston.transports.Console({colorize: true}) ] });
var DOMParser = require('xmldom').DOMParser;
var XMLSerializer = require('xmldom').XMLSerializer;
var D = new DOMParser().parseFromString('
logger.info('Original element value= ' + D.getElementsByTagName('el')[0].childNodes[0].nodeValue); D.getElementsByTagName('el')[0].childNodes[0].nodeValue = 'updated'; logger.info('Updated element value= ' + D.getElementsByTagName('el')[0].childNodes[0].nodeValue);
logger.info('Serialized updated D = ' + new XMLSerializer().serializeToString(D));
Ooops... forgot about preview markdown parsing... zapped my XML string in the parseFromString() command... was a simple 'sample' root with an 'el' element set to 'dummy'.
yes it's a bug, and we can not fixed it for a long time, because of the ie6/7/8 can not support javascript getter and setter.
Thanks. I work around it the long way... using 'replaceChild' --pier On 02/10/12 09:42, jindw wrote:
yes it's a bug, and we can not fixed it for a long time, because of the ie6/7/8 can not support javascript getter and setter.
— Reply to this email directly or view it on GitHub https://github.com/jindw/xmldom/issues/33#issuecomment-9071047.
@jindw I don't really know exactly what's the point being discussed here, but perhaps this article may be of help. http://johndyer.name/native-browser-get-set-properties-in-javascript/
replaceChild
isn't working either for replacing the text node; it's still serialized with the original value. Neither does changing textContent
.
Instead of:
node.nodeValue = "New value";
I did the following:
var parentNode = node.parentNode;
parentNode.removeChild(node);
var newElm = doc.createTextNode("New value");
parentNode.appendChild(newElm);
And it works fine!
The root of this issue seems to be that when using nodeValue =
the data
property is not updated simultaneously. Check my comment here https://github.com/jindw/xmldom/issues/116#issuecomment-510321502