0
votes

I am not able to modify my xml using XMLSerializer... My code is

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

I am getting XML with same old value not 12345.

Please let me know how to resolve this..tried all the options..still not working.:(

2

2 Answers

0
votes

Try changing line 5 to

document.getElementsByTagName("a:Value")[0].childNodes[0].data = '124241';

Edit

Here is the code I wrote at "runkit.com" to demonstrate how I solve the problem as mentioned in the comment.

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

The relevant output from 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>
0
votes

Thanks for replying again ..appricited!!!

I have found the solution,pasting here hopefully it will help others used textContent property change below line

document.getElementsByTagName( "a:Value" ).nodeValue = 12345;

to

document.getElementsByTagName( "a:Value" )[0].textcontent = 12345;

it worked!!! now I am getting the entire XML with changed values. cheers!!