help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Troubles in Regular Expression Paradise


From: Len Blanks
Subject: Re: Troubles in Regular Expression Paradise
Date: Wed, 14 May 2014 17:07:11 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (darwin)

Nicolas Richard <theonewiththeevillook@yahoo.fr> writes:

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

I need to do a better job researching what is available, rather than
reinventing the wheel.  Thanks very much; I'll redo what I have using
libxml and your code to compare and learn.

Regards,
-- 
Len

Je suis Marxiste - tendance Groucho
            -- Slogan used at Nanterre in Paris, 1968




reply via email to

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