unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Nicolas Richard <theonewiththeevillook@yahoo.fr>
To: help-gnu-emacs@gnu.org
Subject: Re: Troubles in Regular Expression Paradise
Date: Wed, 14 May 2014 14:40:05 +0200	[thread overview]
Message-ID: <87oaz0ilei.fsf@geodiff-mac3.ulb.ac.be> (raw)
In-Reply-To: <m2vbt9qezx.fsf@haruspex.net> (Len Blanks's message of "Tue, 13 May 2014 21:18:58 -0500")

Len Blanks <ltb@haruspex.net> writes:
> I'm trying to parse an xml file containing information for a song "currently playing"
> on my iTunes - specifically the artist, name of the tune and the CD in appears on.

> and here is a function I had hoped would strip the relevant fields and return a string in
> the form: "_artist_'s _title_ from the CD _album_" to be inserted in a X-NOW-PLAYING:
> header in emails and usenet posts:

The regexp question was answered, so I allow myself to mention
libxml-parse-xml-region instead of regexps for parsing xml.

First eval:
(setq yftest (libxml-parse-html-region (point-min) (point-max)))
in a buffer which holds your file.

Then you can get away with:
(caddr (assoc 'title (caddr yftest)))
(caddr (assoc 'artist (caddr yftest)))
(caddr (assoc 'album (caddr yftest)))
to get the title, artist and album respectively.

As a side note, I wrote some elisp for using the kind of data that
libxml-parse-html-region spits out, so here's my way of solving your
problem with my code :

(require 'tree-html)
;; https://github.com/YoungFrog/tree-html/blob/master/tree-html.el

(defun yf/get-value-for-sole-subtree-with-given-tag (tree tag)
  "Assume there's exactly one XML element with given TAG in TREE, and return its
associated value."
  (yf/tree-html-get-value
   (yf/tree-html-get-sole-element
    (yf/tree-html-select
     tree
     (lambda (tree)
       (eq tag (yf/tree-html-get-tag tree)))))))

(format "%s's %s from the CD %s"
        (yf/get-value-for-sole-subtree-with-given-tag yftest 'artist)
        (yf/get-value-for-sole-subtree-with-given-tag yftest 'title)
        (yf/get-value-for-sole-subtree-with-given-tag yftest 'album))

(Yes, I'm *that* bad at naming things.)

HTH,

-- 
Nico.



  reply	other threads:[~2014-05-14 12:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-14  2:18 Troubles in Regular Expression Paradise Len Blanks
2014-05-14 12:40 ` Nicolas Richard [this message]
2014-05-14 22:07   ` Len Blanks
     [not found] <mailman.1280.1400034904.1147.help-gnu-emacs@gnu.org>
2014-05-14  2:56 ` Joost Kremers
2014-05-14  3:13   ` Len Blanks

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87oaz0ilei.fsf@geodiff-mac3.ulb.ac.be \
    --to=theonewiththeevillook@yahoo.fr \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).