Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在nodejs中使用xmlbuilder包中的内置streamWriter_Node.js_Xml - Fatal编程技术网

Node.js 在nodejs中使用xmlbuilder包中的内置streamWriter

Node.js 在nodejs中使用xmlbuilder包中的内置streamWriter,node.js,xml,Node.js,Xml,我想创建XML文件,为此我使用xmlbuilder包。 初始代码工作正常,如下所示: const xmlStructure = { // xml structure }; const root = builder.create(xmlStructure, { encoding: 'utf-8' }); const xml = root.end({ pretty: true }); const readableStream = new Readable(); const writab

我想创建XML文件,为此我使用xmlbuilder包。 初始代码工作正常,如下所示:

const xmlStructure = {
    // xml structure
};

const root = builder.create(xmlStructure, { encoding: 'utf-8' });

const xml = root.end({ pretty: true });

const readableStream = new Readable();
const writableStream = file.createWriteStream();

writableStream
    .on('error', error => {
        throw error;
     })

    .on('finish', async () => {
        // do something in the DB
    });

readableStream._read = () => {
    readableStream.push(xml);
    readableStream.push(null);
};

readableStream.pipe(writableStream);
writableStream.end();
但是我在包的wiki中看到,streamwriter中有一个内置的。 我想将其添加到项目中,并删除我在代码中所做的一些自定义操作

我尝试了以下方法:

const xmlStructure = {
    // xml structure
};

const writableStream = file.createWriteStream();

const writer = xmlbuilder.streamWriter(writableStream);

writableStream
    .on('error', error => {
        throw error;
    })

    .on('finish', async () => {
        // do something in the DB
    });


const root = builder.create(xmlStructure, { encoding: 'utf-8' });

const xml = root.end(writer);

const readableStream = new Readable();

readableStream.pipe(writableStream);

不幸的是,这不起作用,我做错了什么或忘记了什么?

缺少的代码行如下:

const xmlStructure = {
    // xml structure
};

const root = builder.create(xmlStructure, { encoding: 'utf-8' });

const xml = root.end({ pretty: true });

const readableStream = new Readable();
const writableStream = file.createWriteStream();

writableStream
    .on('error', error => {
        throw error;
     })

    .on('finish', async () => {
        // do something in the DB
    });

readableStream._read = () => {
    readableStream.push(xml);
    readableStream.push(null);
};

readableStream.pipe(writableStream);
writableStream.end();
最终结果如下所示:

const xmlStructure = {
    // xml structure
};

const root = builder.create(xmlStructure, { encoding: 'utf-8' });

const writableStream = file.createWriteStream();

const writer = xmlbuilder.streamWriter(writableStream);

const xml = root.end(writer);

writableStream.end();

writableStream
    .on('error', error => {
        throw error;
    })

    .on('finish', async () => {
        // do something in the DB
    });