Raya Nasiri
Raya Nasiri

Reputation: 151

How to remove and update a XML tag in xml2js

How can I edit XML file by xml2js

const fs = require('fs');
const xml2js = require('xml2js');
var fn = 'file.xml';
fs.readFile(fn, function(err, data) {
   parser.parseString(data, function(err, result) {
       //result.data[3].removeChild(); ?????
       //result.date[2].name.innerText = 'Raya'; ?????
   });
});

This is not working!

Upvotes: 2

Views: 4236

Answers (1)

Smile4ever
Smile4ever

Reputation: 3704

To remove a property from a JavaScript object, simply set the property to undefined:

result.name = undefined;

Result from xml2js:

{
    "name": "I will get deleted",
    "items": [
        {
            "itemName": "Item 1",
            "itemLocation": "item1.htm"
        },
        {
            "itemName": "Item 2",
            "itemLocation": "item2.htm",
        }
    ]
}

After setting to undefined:

{
    "items": [
        {
            "itemName": "Item 1",
            "itemLocation": "item1.htm"
        },
        {
            "itemName": "Item 2",
            "itemLocation": "item2.htm",
        }
    ]
}

For you, this would be

result.data[3] == undefined;

Tip: you can use JSON.stringify to help you.

Upvotes: 2

Related Questions