Using node v.0.10.29, Express v4.12.0 and xmldom v0.1.19, I am trying to do the following:
Actions
- Read the XML file in line
- Convert string to XML object using xmldom
- Set the field
<name>default</name>to<name>test</name> - Convert XML object to string
Problem
The problem is that after I set the field <name>, it sets the object correctly, but when I convert it to a string, the field <name>returns to the old value (incorrect).
Code
Here is the code for this:
var fs = require('fs');
var DOMParser = require('xmldom').DOMParser;
var XMLSerializer = require('xmldom').XMLSerializer;
var filename = "myFile.xml";
fs.readFile(filename, "utf-8", function (err,data)
{
var customerConfig = new DOMParser().parseFromString(data);
customerConfig.getElementsByTagName("name")[0].childNodes[0].nodeValue = "test";
console.log(customerConfig.getElementsByTagName("name")[0].childNodes[0].nodeValue);
var xmlString = new XMLSerializer().serializeToString(customerConfig);
console.log(xmlString);
});
, <name> test ... , ? - , ?
!