我正在编写一个简单的原子包。当我发送请求时,服务器会发出 xml 响应,因此我尝试使用以下命令解析它xml2js https://github.com/Leonidas-from-XIV/node-xml2js。但是出现错误:
错误:第一个标签之前没有空格。行:0 列:1 字符:4
err https://i.stack.imgur.com/lvm9p.png
我该如何解决?
先感谢您。
部分代码:
module.exports = class HatenaBlogPost
~~~
@hatenaBlogPost = new HatenaBlogPost()
~~~
postEntry: (callback) ->
draft = if @isPublic then 'no' else 'yes'
requestBody = """
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:app="http://www.w3.org/2007/app">
<title>#{@entryTitle}</title>
<author><name>#{@getHatenaId()}</name></author>
<content type="text/plain">
#{_.escape(@entryBody)}
</content>
<updated>#{moment().format('YYYY-MM-DDTHH:mm:ss')}</updated>
<app:control>
<app:draft>#{draft}</app:draft>
</app:control>
</entry>
"""
options =
hostname: 'blog.hatena.ne.jp'
path: "/#{@getHatenaId()}/#{@getBlogId()}/atom/entry"
auth: "#{@getHatenaId()}:#{@getApiKey()}"
method: 'POST'
request = https.request options, (res) ->
res.setEncoding "utf-8"
body = ''
res.on "data", (chunk) ->
body += chunk
res.on "end", ->
callback(body)
request.write requestBody
request.end()
View:
{parseString} = require 'xml2js'
~~~
@hatenaBlogPost.postEntry (response) =>
parseString response, (err, result) =>
if err
atom.notifications.addError("#{err}", dismissable: true)
else
entryUrl = result.entry.link[1].$.href
entry_Title = result.entry.title
atom.notifications.addSuccess("Posted #{entry_Title} at #{entryUrl}", dismissable: true)
罪魁祸首是所谓的字节顺序标记 (BOM),它是一个 3 字节的“零宽度不间断空格”Unicode 字符,Windows 系统自动将其添加到 UTF-8 文件中。使用十六进制编辑器检查文件时,BOM 显示为十六进制EFBBBF
.
要解决此问题:
var cleanedString = origString.replace("\ufeff", "");
See 本文 http://www.multiasking.com/blog/xml2js-sax-js-non-whitespace-before-first-tag/了解更多。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)