Reputation: 4182
I am using nodejs xmldom module to call a SOAP service. Before calling the service, I have to set the phone number
The code is as follows
var fs = require('fs');
var xmldoc = fs.readFileSync('req.xml', 'utf8');
var DOMParser = require('xmldom').DOMParser;
var doc = new DOMParser().parseFromString(
xmldoc
,'text/xml');
//change phone number from xxxxxx to 83834324838 (dummy)
doc.getElementsByTagName('msisdn').item(0).firstChild.nodeValue = '83834324838';
var phone = doc.getElementsByTagName('msisdn').item(0).firstChild.nodeValue;
// logs 83834324838
console.log(phone);
// serialize DOM back to xml
var XMLSerializer = require('xmldom').XMLSerializer;
var serializedXML = new XMLSerializer().serializeToString(doc);
var inspect = require('util').inspect;
//!!serialized XML doesnt have the 83834324838
console.log(inspect(serializedXML, {
colors: true,
depth: Infinity
}));
//call to SOAP service
The variable serializedXML does not have the phone number that I set (83834324838). I checked DOM documentation and I see that the way I am setting the msisdn/phone_number is right (console.log(phone) proves that). But once serialize the DOM back into XML string, I just lose the new number.
At wits end here. Can anyone please take a stab at this?
Upvotes: 1
Views: 2593
Reputation: 49
I faced the same issue. Did some debugging of the xmlnode module and it worked for me very well ...
var node = xmlDoc.getElementsByTagName(nodeName)[0];
if (node) {
var textNode = node.childNodes[0];
if (!textNode) {
textNode = xmlDoc.createTextNode("");
node.appendChild(textNode);
}
textNode.nodeValue = val;
textNode.data = val.toString();
return true;
}
Upvotes: 0
Reputation: 249
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!
Upvotes: 1
Reputation: 4182
Ok, as Tim has already pointed out, there is a bug in xmldom that stands unresolved so far. This is how I circumvented the bug.
var fs = require('fs');
var DOMParser = require('xmldom').DOMParser;
var XMLSerializer = require('xmldom').XMLSerializer;
var request = require('request');
var xml = fs.readFileSync('soap_reqs/req.xml', 'utf8');
var serializedXML = manipulateDOM({
xml: xml,
nodes: [{
parent: 'parent_node',
child: 'phone_number',
value: '83834324838'
}, {
parent: 'another_parent_node_whose_child_you_want_to_change',
child: 'childnode',
value: 'xmlsucks'
}
]
});
var inspect = require('util').inspect;
console.log(inspect(serializedXML, {
colors: true,
depth: Infinity
}));
Upvotes: 1
Reputation: 324477
It's a bug in the XMLDOM module:
https://github.com/jindw/xmldom/issues/33
I imagine there are other Node.js modules that do the job. How about libxmljs? (Disclaimer: found through a quick search. I've never used it.)
Upvotes: 2