xmldom icon indicating copy to clipboard operation
xmldom copied to clipboard

Serialize fails to account for text node updates

Open braytak opened this issue 12 years ago • 7 comments

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('dummy');

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));

braytak avatar Aug 21 '12 13:08 braytak

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'.

braytak avatar Aug 21 '12 13:08 braytak

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.

jindw avatar Oct 02 '12 13:10 jindw

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.

braytak avatar Oct 02 '12 14:10 braytak

@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/

arboleya avatar Dec 30 '12 04:12 arboleya

replaceChild isn't working either for replacing the text node; it's still serialized with the original value. Neither does changing textContent.

DullReferenceException avatar Jan 19 '15 00:01 DullReferenceException

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!

fruityfred avatar Jul 31 '15 07:07 fruityfred

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

cburatto avatar Jul 11 '19 04:07 cburatto