如何从emacs中的URL解析xml?

时间:2012-11-12 03:22:10

标签: xml url emacs

我正在使用url-retrieve-synchronously函数,它返回一个包含响应的缓冲区。我想知道是否有一种非hacky方式来获取响应的主体并能够将其传递给xml-parse-region。

是否有xml-parse-from-url函数?如何从url-retrieve解析响应?

2 个答案:

答案 0 :(得分:0)

您可以查看一些与Emacs一起分发的软件包是如何做到的。例如,lisp/net/newst-backend.ellisp/net/soap-client.el

答案 1 :(得分:0)

在identica-mode.el中(根据GNU GPL版本2获得许可),有一个函数将使用url-retrieve并将其传递给xml解析器并返回解析后的响应:

(defun identica-get-response-body (&optional buffer)
  "Extract HTTP response body from HTTP response, parse it as XML, and return a XML tree as list.
`buffer' may be a buffer or the name of an existing buffer.
 If `buffer' is omitted, current-buffer is parsed.

Removed the call to `identica-clean-response-body'."
  (or buffer
      (setq buffer (current-buffer)))
  (set-buffer buffer)
  (set-buffer-multibyte t)
  (let ((start (save-excursion
         (goto-char (point-min))
         (and (re-search-forward "<\?xml" (point-max) t)
              (match-beginning 0)))))
    ;;(identica-clean-response-body) ; necessary for identica, cleaned up some weird characters
    (and start
         (xml-parse-region start (point-max)))))