xmldom
xmldom copied to clipboard
Parse and modify XML tag element using xmldom
I am trying to read modify write and XML tag element value.
fs.readFile('./test.xml','utf8', function (err, data) { if (err) { return console.log(err); } else { var doc = new dom().parseFromString(data);
doc.getElementsByTagName("status")[0].childNodes[0].nodeValue = '100';
//Below statement return correct value 100
console.log(doc.getElementsByTagName("status")[0].childNodes[0].nodeValue);
var serializer = new xmldom.XMLSerializer();
var writetofile = serializer.serializeToString(doc);
//Returns WRONG old value for the modified childNode.
console.log(writetofile);
}
});
Am I missing something ?
Thanks in advance.
I'm seeing the same issue, I posted a similar explanation here: http://stackoverflow.com/questions/30571674/replacing-text-in-an-xml-node-in-node-js.
The problem seems to be with the XMLSerializer, I can drill down to the same nodeValue result numerous times and get the correct output, but as soon as I serialise the document the node text is reverted to the original value.
Same problem for me :( Is there any chance to get this fixed quickly? Or is there a simple workaround? Thanks!
Folks, it is 2019 and I am having this issue too. Any updates? If you print the node object (instead of serializing it) you get a discrepancy.
In my case I am using childNodes.item(0).nodeValue
to change the existing value English
to Portuguese
;
If you print the node before changing, you have
Text {
data: 'English',
nodeValue: 'English',
length: 7,
lineNumber: 1223,
columnNumber: 51,
But after changing, I notice that the data
property is not modified.
Text {
data: 'English',
nodeValue: 'Portuguese',
length: 7,
lineNumber: 1223,
columnNumber: 51,
Any way to access the property and modify it too?
A related issue with a workaround here: https://github.com/jindw/xmldom/issues/33#issuecomment-126597795
Hello! It's the year 2023 and I'm experiencing the same issue.
The issue does, indeed, lie in that changing the nodeValue doesn't automatically update the data value.
So I decided to edit the data value as well with satisfying results:
node.firstChild.nodeValue = text;
node.firstChild.data = text;
this project has not been maintained for years, but the fork https://github.com/xmldom/xmldom which is published as @xmldom/xmldom
has more recent activity.
Even though this issue has also not been addressed in that repo, and it's not a trivial change since it's using plain fields all over the place instead of getters, it might be worth porting the issue over there, so it can be prioritized.