R01010010
R01010010

Reputation: 5948

Open, read, change and save an XML in node.js

I'm trying to open, read, change and save an xml in node.js I'm using XMLDoc but i'm stuck at the change and save phase.

Given this XML:

<widget version="1.0.0">
    <!-- NAME -->
    <name short="Name-en"></name>
</widget>

I want to open this config.xml file and set foo as content.

fs.readFile(__dirname + '/templates/widget_template/config.xml', 'utf8', function (err, data) {
    if (err)  return console.log(err);

    var document = new xmldoc.XmlDocument(data);
    document.descendantWithPath("name").value = 'foo';
    console.log(document.toString());
});

But when i do the console.log(document.toString()) what I get is:

<widget version="1.0.0">
  <name short="Name-en"/>
</widget>

And what i'm trying to get is:

<widget version="1.0.0">
  <name short="Name-en">foo</name>
</widget>

Am I doing something wrong?, is there a better way to do this than using XMLDoc?, thanks!

Upvotes: 0

Views: 597

Answers (1)

R01010010
R01010010

Reputation: 5948

I'm stupid, just changed

document.descendantWithPath("name").value = 'foo';

for

document.descendantWithPath("name").val = 'foo';

and it worked.

Upvotes: 1

Related Questions