如何删除和更新 xml2js 中的 XML 标签

How to remove and update a XML tag in xml2js

如何通过 xml2js

编辑 XML 文件
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'; ?????
   });
});

这是行不通的!

要从 JavaScript 对象中删除 属性,只需将 属性 设置为未定义:

result.name = undefined;

xml2js 的结果:

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

设置为undefined后:

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

对你来说,这将是

result.data[3] == undefined;

提示:您可以使用JSON.stringify来帮助您。