0

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!

1 Answer 1

1

I'm stupid, just changed

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

for

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

and it worked.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.