[erlang-questions] Parsing XML with xmerl_xpath/string/2

lloyd <>
Wed Apr 15 22:15:20 CEST 2015


Well, pick myself up from one stumble only to trip over the next.

I've successfully downloaded an XML book file from Amazon and extracted title and author code using this function, much of which I borrowed from Dave Thomas:

get_book_data(ISBN) ->
   {ok, {_Status, _Headers, Body}} = httpc:request(get_book_request(ISBN)),
   {Xml, _Rest} =  xmerl_scan:string(Body),
   [ #xmlText{value=Author}] = xmerl_xpath:string("//Author/text()", Xml),
   [ #xmlText{value=Title}] = xmerl_xpath:string("//Title/text()", Xml),
   [ #xmlText{value=Publisher}] = xmerl_xpath:string("//Manufacturer/text()", Xml),
   {Author, Title, Publisher}.

That same file has URLs for downloading book cover images. The XML looks like this:

...
<LargeImage>
  <URL>
      http://ecx.images-amazon.com/images/I/51shI6vQ-SL.jpg
  </URL>
      <Height Units="pixels">500</Height>
      <Width Units="pixels">333</Width>
</LargeImage>
...

Modifying get_book_data/1 to include a xmerl_xpath:string/2 call that looks like this:

   [ #xmlText{value=LargeImage}] = xmerl_xpath:string("//LargeImage/text()", Xml),

fails:

** exception error: no match of right hand side value []
     in function  amz_lookup:get_book_cover_images/1 (/home/lloyd/wga/site/src/LitUtils/amz_lookup.erl, line 110)

My hunch is that the string "//LargeImage/text())" is the problem. xmerl_xpath:string/2 docs give me no comfort. Clearly I don't understand them sufficiently.

I've tried xmerl_xpath:string("//URL/text()"), xmerl_xpath:string("//LargeImage/URL/text()"), etc. etc. etc.--- all to no avail. Google search also comes up empty.

So once more to the well of wisdom. Can some kind soul point out the error of my ways?

All the best,

LRP















More information about the erlang-questions mailing list