user1409364
user1409364

Reputation:

Image Slider for Normal Data to xml data

I am using a normal variable to display the list of images. But the same thing I need is by using the XML. Can anyone please help me. The var 'imgs' should be an xml.

Jquery

$(function() {

    var imgs = ['http://www.academy-florists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg', 'http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg', 'http://www.behok.ru/i/a/cat/gerbera.jpg', 'http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg', 'http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg'];

   var maximages = imgs.length; //No of Images
   Slider();
   setInterval(Slider, 3000);
   var prevIndex = 0, prevPrevIndex = 0;

   function Slider() {
       $('#imageSlide').fadeOut("slow", function() {
           do {
               shuffleIndex = Math.floor(Math.random() * maximages);
           } while(prevIndex == shuffleIndex || prevPrevIndex == shuffleIndex)

           prevPrevIndex = prevIndex;
           prevIndex = shuffleIndex;

           $("#panel").fadeIn("slow").css('background', '#000');

           $(this).attr('src', imgs[shuffleIndex]).fadeIn("slow");
       });
   }

});

Upvotes: 0

Views: 303

Answers (2)

Jacob George
Jacob George

Reputation: 2627

I am assuming you want the image urls in xml format and then you want to parse this xml.

<images>
<image-url>http://www.academyflorists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg</image-url>
<image-url>http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg</image-url>
<image-url>http://www.behok.ru/i/a/cat/gerbera.jpg</image-url>
<image-url>http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg</image-url>
<image-url>http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg</image-url>
</images>

The you can parse it in the following way

var imgs = "<images><image-url>'http://www.academy-florists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg'</image-url><image-url>'http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg'</image-url><image-url>'http://www.behok.ru/i/a/cat/gerbera.jpg'</image-url><image-url>'http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg'</image-url><image-url>'http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg'</image-url></images>"
$xml = $.parseXML(imgs)
images = []
$($($xml).find('image-url')).each(function() { images.push($(this).text()) })

images will contain an array of all the image urls

Upvotes: 2

Chris Moutray
Chris Moutray

Reputation: 18379

You haven't told us what your xml would look like so I've together the following basic schema

<?xml version="1.0" encoding="utf-8" ?>
<images>
    <image>url of image</image>
    <image>url of next image</image>
<images>

If you search the jQuery site you will find that it tells you how to work with Xml - http://api.jquery.com/jQuery.parseXML/

You need to use parseXML on your xml string and convert to a jQuery object. You can then find() elements, loop through elements or pickout the elements text and attributes. The following example shows this.

$(function() {

    // your image list as xml string
    var xmlStr = '<?xml version="1.0" encoding="utf-8" ?><images><image>http://www.academy-florists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg</image><image>http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg</image><image>http://www.behok.ru/i/a/cat/gerbera.jpg</image><image>http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg</image><image>http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg</image></images>';

    // parse the xml and produce an jQuery image object ($image) to represent the list of image elements
    var xmlDoc = $.parseXML(xmlStr),
        $xml = $(xmlDoc),
        $image = $xml.find("image");

    // loop throught your image elements and alert the url
    $image.each(function() {
        var imageElement = this, // this within the loop is the current element
            $imageElement = $(imageElement),
            imageUrl = $imageElement.text(); // use text to get the url

        alert(imageUrl);
    });

    // use length on the jQuery object to get the element count
    var maximages = $image.length;
    alert(maximages);

    var shuffleIndex = 3,
        imageElement = $image[shuffleIndex], // index the random image that you want to use
        $imageElement = $(imageElement),
        imageUrl = $imageElement.text();

    alert(imageUrl);

});

Upvotes: 1

Related Questions