如何使用节点js在XML文件中添加/修改节点

时间:2021-11-11 19:33:57

Let's say I have the following xml code

假设我有以下xml代码

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<catalog>
 <person>
    <name>John</name>
    <surname>Smith</surname>
 </person>
 <person>
    <name>Abe</name>
    <surname>Lincoln</surname>
  </person>
  <person>
    <name>James</name>
    <surname>Bond</surname>
  </person>
</catalog>

And I want to add a new node, let's say the following:

我想添加一个新节点,让我们说以下内容:

     <person>
        <name>Tony</name>
        <surname>Stark</surname>
     </person>

How do I do this in node js? I mean I have a file (/routes/index.js in node js express, to be exact), and I want to be able to add/modify existing xml file. I've tried fs.writeFile(), but this writes a whole new file, and fs.appendFile() adds a header(?xml + encoding and stuff) and root nodes after the last node, so I can't insert it into the catalog node. I can't get rid of the xml declaration header either.
I've been using .builder() to do this, and it looked like this

我如何在节点js中执行此操作?我的意思是我有一个文件(在节点js express中是/routes/index.js,确切地说),我希望能够添加/修改现有的xml文件。我已经尝试了fs.writeFile(),但这会写一个全新的文件,而fs.appendFile()会在最后一个节点之后添加一个标题(?xml +编码和东西)和根节点,所以我无法插入它进入目录节点。我也无法摆脱xml声明标头。我一直在用.builder()来做这件事,看起来像这样

router.post('/addnode', function(req,res) {

var obj = {name: "Tony", surname: "Stark"};
var fs = require('fs');
var xml2js = require('xml2js');

var builder = new xml2js.Builder();
var xml = builder.buildObject(obj);  

fs.writeFile('public/test.xml', xml, function (err)
if (err) throw err;
    console.log('It\'s saved!');
}); 
});

Finally I want to get that data from a from addnode, so I won't create a var obj in this function.

最后我想从addnode获取数据,所以我不会在这个函数中创建一个var obj。

1 个解决方案

#1


2  

Unless the files' sizes are unacceptable and you cannot read them as a whole, you can use xml2js and never work with XML.

除非文件的大小不可接受且您无法将它们作为一个整体读取,否则您可以使用xml2js并且永远不会使用XML。

By using it, you can read the file, convert it in an object which is far easier to manipulate, add your stuff there and then convert it back as XML and write the result again on the original file.

通过使用它,您可以读取文件,将其转换为更容易操作的对象,在那里添加您的东西,然后将其转换回XML并将结果再次写入原始文件。

Looking at your example, you create a new object and append it to the the file. Instead, you should read the already existent data and modify them, then write everything back (do not append them) to the file. To do that, you have to use the Parser object from xml2js.

查看您的示例,您将创建一个新对象并将其附加到该文件。相反,您应该读取已存在的数据并对其进行修改,然后将所有内容写回(不要将它们附加到)文件中。为此,您必须使用xml2js中的Parser对象。

#1


2  

Unless the files' sizes are unacceptable and you cannot read them as a whole, you can use xml2js and never work with XML.

除非文件的大小不可接受且您无法将它们作为一个整体读取,否则您可以使用xml2js并且永远不会使用XML。

By using it, you can read the file, convert it in an object which is far easier to manipulate, add your stuff there and then convert it back as XML and write the result again on the original file.

通过使用它,您可以读取文件,将其转换为更容易操作的对象,在那里添加您的东西,然后将其转换回XML并将结果再次写入原始文件。

Looking at your example, you create a new object and append it to the the file. Instead, you should read the already existent data and modify them, then write everything back (do not append them) to the file. To do that, you have to use the Parser object from xml2js.

查看您的示例,您将创建一个新对象并将其附加到该文件。相反,您应该读取已存在的数据并对其进行修改,然后将所有内容写回(不要将它们附加到)文件中。为此,您必须使用xml2js中的Parser对象。