gpt4 book ai didi

node.js - 在nodejs中读取xml文件

转载 作者:太空宇宙 更新时间:2023-11-03 22:00:34 28 4
gpt4 key购买 nike

我有一个具有以下结构的本地文件:

<?xml version='1.0' encoding='UTF-8'?>
<foxydata>
<store_version>2.0</store_version>
<result>ERROR</result>
<messages>
<context>2008</context>
<message>Transaction Not Found (transaction_id:25)</message>
</messages>
</foxydata>

我想读取该文件并将其返回到浏览器,所以我的代码如下:

options = 
encoding = 'UTF8'

fs.readFileSync(filepathXML, options, (err, data) ->
throw err if err
return data
)

我可以读取该文件,但浏览器上的结果如下:

2.0SUCCESSTransaction Found

知道是什么导致了问题或者如何读取 xml 文件,我应该在 readFile 上定义其他内容吗?

最佳答案

readFileSync没有第三种选择。也许你的意思是 readFile

1.

    var fs = require('fs');
console.log(fs.readFileSync('./test.xml', {encoding: 'utf-8'}));

2.

    var fs   = require('fs');
var http = require('http');

http.createServer(function (req, res) {
res.setHeader('Content-Type', 'text/xml');
res.end(fs.readFileSync('./test.xml', {encoding: 'utf-8'}));
}).listen(8811);

3.

    var express = require('express'),
app = express();

app.get('/', function (req, res) {
res.set('Content-Type', 'text/xml');
res.send(fs.readFileSync('./test.xml', {encoding: 'utf-8'}))
})

app.listen(3000);

关于node.js - 在nodejs中读取xml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26753638/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com