0

Is this the most efficient way to retrieve these xml elements using php? It is not too long but the three nested loops seems a bit weighty and just doesn't feel right.

Thank you very much, Todd

// get the existing xml file
$url = '../../page_xml/index.xml';
$xml = simplexml_load_file($url); // == Root tag

// get the sliderImages node
foreach($xml->Page as $page){
    foreach($page->sliderImages as $sliderImages){
        foreach($sliderImages->sliderImage as $sliderImage){
            echo "<strong>Title:</strong> ".$sliderImage."<br/>";

        }
    }
}

EDIT:

Here is the xml:

<?xml version="1.0" encoding="utf-8" ?>
<Root type="content">
  <Page>
    <hMenuHighlight>home</hMenuHighlight>
    <pageName>Home</pageName>

    <sliderImages>
        <sliderImage>images/home_slider/paper_bag.jpg</sliderImage>
        <sliderImage>images/home_slider/red_box.jpg</sliderImage>
        <sliderImage>images/home_slider/cubes.jpg</sliderImage>
        <sliderImage>images/home_slider/gift_boxes.jpg</sliderImage>        
    </sliderImages>


    <pageText>
    <![CDATA[
        <h1>Welcome to myShopName</h1>

        <!-- Start SLIDER IMAGES -->
        <div id="slideshow">
            <!--##mySlideshowImages-->
        </div>
        <!-- End SLIDER IMAGES -->

        <p>Puzzle Logo image provided by <a href="http://www.logoease.com">Logoease</a>.</p>
        <p>Paper bag image provided by <a href="http://www.freedigitalphotos.net/images/view_photog.php?photogid=1012">Felixco, Inc. / FreeDigitalPhotos.net</a>.</p>
        <p>Red Box image provided by <a href="http://www.freedigitalphotos.net/images/view_photog.php?photogid=1058">Arvind Balaraman / FreeDigitalPhotos.net</a>.</p>
        <p>Cubes image provided by <a href="http://www.freedigitalphotos.net/images/view_photog.php?photogid=1152">jscreationzs / FreeDigitalPhotos.net</a></p>
        <p>Gift boxes image provided by <a href="http://www.freedigitalphotos.net/images/view_photog.php?photogid=1152">jscreationzs / FreeDigitalPhotos.net</a></p>

        <p>Lorem ipsum dolor.</p>

    ]]>
    </pageText>
  </Page>
</Root>
3
  • 1
    Can you post a sample of the XML? 3 nested loops seems bulky. Commented Nov 8, 2011 at 20:40
  • It is bulky, the xml needs to be "deep" to work with a couple of add ons I am using. Commented Nov 8, 2011 at 22:11
  • I guess a more fitting term would have been condensed. Commented Nov 9, 2011 at 2:13

2 Answers 2

1

Since simplexml_load_file returns an object, you can do:

foreach ( $xml->Page->sliderImages->sliderImage as $sliderImage )
Sign up to request clarification or add additional context in comments.

Comments

1

If there is only one <Page> tag and one <sliderImages> tag, then you can do:

$si_list = $xml->Page->sliderImages;
foreach($si_list as $si)
    echo "<strong>Title:</strong> " . $si . "<br/>";

1 Comment

Thank you for the answer. This is what I was envisioning. I chose Ahmed's answer because it is more efficient,less lines, etc. I think this one is easier to "follow" though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.