Creating a list of links from XML file/Wrapping text with anchor tags with href attributes

Creating a list of links from XML file/Wrapping text with anchor tags with href attributes

Hello,

I am trying to solve this problem using jQuery:

Broadly speaking, I have an xml file of URLs and Titles that I would like to use to build a list of links in this format:
  1. <p><a href="url1">Title1</a></p>
  2. <p><a href="url2">Title2</a></p>
and so on ...

The xml looks like this:

  1. <item n="1">
  2.     <link>http://www.mysite.com/page1</link>
  3.     <title>My Site Page 1</title>
  4. </item>
and so on.

The function to parse the XML is (post AJAX call):

  1.          function parseSearch (xml) {
  2.              $(xml).find(item).each(function() {
  3.                 var link = $(this).find("link").text();
  4.                 var title = $(this).find("title").text();
  5.                
  6.                 $("#output").append(title);
  7.                 $("#output").append(link);
  8.              });
  9.          }


which lumps everything together, title , followed by link for all items in the XML into <div id="output"></div>. So far, so good.

I have tried these without success to format the output:

  1.     $("#output").append(title).wrap("<a><\/a>").attr("href", link);
  2.     $("#output").append(title).wrapInner("<a href=''><\/a>").attr("href", link);


This returns a nested array of anchor tags, which clearly not the desired result.
  1.     $("#output").append(title).wrapInner("<a href="+link+"><\/a>");


I feel I am really close, but can't seem to find the missing link. Any
suggestions, pointers to documentation I may have missed, similar examples, etc, that
could help solve the problem are most welcome.

Thanks!

KM