Created
September 29, 2016 02:57
-
-
Save crimeminister/2955d72c5c848814ae6cb567dd9bd092 to your computer and use it in GitHub Desktop.
A simple example of XML processing in Emacs-Lisp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(let* ((xml "<post time=\"20050716234509\" id=\"010101\"> | |
<login>Test</login> | |
<msg>Here is the message</msg> | |
<info>My UA</info> | |
</post>") | |
(root (with-temp-buffer | |
(insert xml) | |
(xml-parse-region (point-min) (point-max)))) | |
(post (car root)) | |
(login (car (xml-get-children post 'login))) | |
(login-user (car (xml-node-children login))) | |
(attrs (xml-node-attributes post)) | |
(time (cdr (assq 'time attrs))) | |
(msg (car (xml-get-children post 'msg))) | |
(text (car (xml-node-children msg)))) | |
(message "time: %s, message: '%s', login: '%s'" time text login-user)) | |
(defun get-yards () | |
(let* ((root (xml-parse-region (point-min) (point-max))) | |
(golf-tourney (car root)) | |
(yardage (car (xml-get-children golf-tourney 'yardage))) | |
(@yardage (xml-node-attributes yardage)) | |
(yards (cdr (assq 'yards @yardage)))) | |
yards)) | |
(defun show-yards () | |
"Show the number of yards for a course." | |
(interactive) | |
(let* ((yards (get-yards))) | |
(message "yards: %s" yards))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun get-url-attribute (feed-node) | |
(let* ((attrs (xml-node-attributes feed-node)) | |
(url (cdr (assq 'source attrs)))) | |
url)) | |
(defun get-feed-list () | |
(let* ((root (xml-parse-region (point-min) (point-max))) | |
(feeds (car root)) | |
(feed-list (xml-get-children feeds 'feed)) | |
(feed-urls (map 'list 'get-url-attribute feed-list))) | |
feed-urls)) | |
(defun insert-elements-of-list (list) | |
"Insert each element of LIST on a line of its own." | |
(while list | |
(insert (car list)) | |
(insert "\n") | |
(setq list (cdr list)))) | |
(defun extract-source-attrs () | |
"Show the number of yards for a course." | |
(interactive) | |
(let* ((feeds (get-feed-list))) | |
(set-buffer "*urls*") | |
(insert-elements-of-list feeds))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment