如何通过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对象中删除属性,只需将属性设置为undefined即可:

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来帮助您。

关于node.js - 如何在xml2js中删除和更新XML标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39225499/

10-10 04:58