[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Troubles in Regular Expression Paradise

From: Nicolas Richard
Subject: Re: Troubles in Regular Expression Paradise
Date: Wed, 14 May 2014 14:40:05 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.91 (gnu/linux)

Len Blanks <> 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 
> 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)

(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."
     (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.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]