ura*_*way 5 xml-parsing coffeescript
我正在编写一个简单的原子包。当我发送请求时,服务器会发出 xml 响应,因此我尝试使用xml2js解析它。但是出现错误:
错误:第一个标签之前没有空格。行:0 列:1 字符:4
我该如何解决?先感谢您。
部分代码:
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()
Run Code Online (Sandbox Code Playgroud)
看法:
{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)
Run Code Online (Sandbox Code Playgroud)
罪魁祸首是所谓的字节顺序标记(BOM),它是一个 3 字节 \xe2\x80\x9c 零宽度不间断空格 \xe2\x80\x9d Unicode 字符,Windows 系统自动将其添加到 UTF-8 文件中。使用十六进制编辑器检查文件时,BOM 显示为 hex EFBBBF。
要解决该问题:
\n\nvar cleanedString = origString.replace("\\ufeff", "");\nRun Code Online (Sandbox Code Playgroud)\n\n请参阅这篇文章了解更多信息。
\n| 归档时间: |
|
| 查看次数: |
28610 次 |
| 最近记录: |