-4

I have a XML file as follows:

<bracelets>
     <photo filename="b1.jpg" thumbnail="a1.jpg" description="aa" />
     <photo filename="b2.jpg" thumbnail="a2.jpg" description="aa" />
     <photo filename="b3.jpg" thumbnail="a3.jpg" description="aa" />
     <photo filename="b4.jpg" thumbnail="a4.jpg" description="aa" />
</bracelets>

I want to fetch all image names into a .php page.

I am using this now.

$bracelets = simplexml_load_file($bracelets_xml);
        x = xmlDoc.getElementsByTagName('photo')[0].attributes;
        for (i = 0; i < x.length; i++) {
            document.write(x[i].childNodes[0].nodeValue);
        }​

But currently I am only able to fetch one image.

How can I fetch all images instead?

gymcode
  • 4,431
  • 15
  • 72
  • 128
Wazan
  • 539
  • 1
  • 8
  • 27
  • 5
    Do you understand the code you have written? I think you should start with figuring out what each line does, because that will instantly answer your question. – Tomalak Feb 15 '12 at 06:29
  • Yeah, that `[0]` is pretty much going to limit it to only the first one. – Anthony Feb 15 '12 at 06:38
  • @Tomalak I have got the correct answer from Anthony and edited the question too. But I cant ask anymore question from Stackoverflow because of bad voting the question they blocked me, Please help me to make it work, Thank you n advence.. – Wazan Jun 05 '12 at 09:33
  • @Wazan Please ask for support on http://meta.stackoverflow.com - I am just a normal user and can't help you with this. – Tomalak Jun 05 '12 at 09:48

3 Answers3

1

If PHP is an option, have you looked at SimpleXML yet?

In your case:

    $bracelets_xml = <<<XML
    <bracelets>
            <photo filename="b1.jpg" thumbnail="a1.jpg" description="aa" />
            <photo filename="b2.jpg" thumbnail="a2.jpg" description="aa" />
            <photo filename="b3.jpg" thumbnail="a3.jpg" description="aa" />
            <photo filename="b4.jpg" thumbnail="a4.jpg" description="aa" />
    </bracelets>
XML;

$bracelets = new SimpleXMLElement($bracelets_xml);

foreach($bracelets -> photo as $photo) {
    $counter++;
    echo "Photo " . $counter . ":\r\n";
    echo "Filename : " . $photo['filename'] . "\r\n";
    echo "Thumbnail : " . $photo['thumbnail']. "\r\n";
    echo "Description : " . $photo['description']. "\r\n";
}

Obviously the output above isn't what you want, but you could output it however you want depending on the context.

Anthony
  • 36,459
  • 25
  • 97
  • 163
  • I checked , Im geting the looping Problem – Wazan Feb 15 '12 at 06:34
  • You're getting what looping problem? I could have miswritten something. Are you getting an error? – Anthony Feb 15 '12 at 06:40
  • Actually, I just copied and pasted my own code and your sample xml and it works fine. What looping problem? – Anthony Feb 15 '12 at 06:44
  • $yourxmlstring = simplexml_load_file("bracelets/flashmo_038_thumbnails.xml"); $bracelets = new SimpleXMLElement($yourxmlstring); foreach($bracelets -> photo as $photo) { echo $photo['filename']; echo $photo['thumbnail']; echo $photo['description']; } ?> – Wazan Feb 15 '12 at 06:46
  • I tidied up the output so it should be clearer whether it's working or not. Which it is. – Anthony Feb 15 '12 at 06:51
  • Ahhh. I see. using simple_xml_load_file creates the SimpleXMLElement automatically, so just change your code to: `$bracelets = simplexml_load_file("bracelets/flashmo_038_thumbnails.xml");` and go from there. – Anthony Feb 15 '12 at 06:53
  • im geting an error Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\xampp\htdocs\unixpo\gallery.php:82 Stack trace: #0 C:\xampp\htdocs\unixpo\gallery.php(82): SimpleXMLElement->__construct('') #1 {main} thrown in C:\xampp\htdocs\unixpo\gallery.php on line 82 – Wazan Feb 15 '12 at 07:54
0

Loop through all the elements returned by xmlDoc.getElementsByTagName('photo').

alex
  • 479,566
  • 201
  • 878
  • 984
0

This line:

x=xmlDoc.getElementsByTagName('photo')[0].attributes; 

Gets the first photo. Then you do things with it.

Change that to a for loop like the one you have for looping over the attributes.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335