Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse and modify XML tag element using xmldom #116

Open
youemailmee opened this issue Mar 12, 2015 · 6 comments
Open

Parse and modify XML tag element using xmldom #116

youemailmee opened this issue Mar 12, 2015 · 6 comments

Comments

@youemailmee
Copy link

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.

@seanhodges
Copy link

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.

@fruityfred
Copy link

Same problem for me :(
Is there any chance to get this fixed quickly? Or is there a simple workaround?
Thanks!

@cburatto
Copy link

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?

@cburatto
Copy link

A related issue with a workaround here: #33 (comment)

@designbyadrian
Copy link

designbyadrian commented Mar 10, 2023

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;

@karfau
Copy link

karfau commented Mar 10, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants