Solved

simplexml_load_string() Problem

Posted on 2011-03-25
3
407 Views
Last Modified: 2012-05-11
I've attached the code I am using. I am expecting the following result

Hoover Dam
Helicopter Flights
The London Eye
The 02
Empire State Building
The Statue Of Liberty

What I'm actually getting is

Hoover Dam
Helicopter Flights


Help really appreciated.

<?php

$xmlcontent='<?xml version="1.0" encoding="UTF-8"?>
    <ResultSet size="3">

        <Destination name="Las Vegas area, NV" order="1">
            <ListResults size="2">
                <Attraction order="1">
                <name><![CDATA[Hoover Dam]]></name>
                </Attraction>
                
                <Attraction order="2">
                <name><![CDATA[Helicopter Flights]]></name>
                </Attraction>
            </ListResults>
        </Destination>

        <Destination name="London, GBR" order="2">
            <ListResults size="2">
                <Attraction order="1">
                <name><![CDATA[The Londo Eye]]></name>
                </Attraction>
                
                <Attraction order="2">
                <name><![CDATA[The 02]]></name>
                </Attraction>
            </ListResults>

        </Destination><Destination name="New York, NY" order="3">
            <ListResults size="2">
                <Attraction order="1">
                <name><![CDATA[Empire State Building]]></name>
                </Attraction>
                
                <Attraction order="2">
                <name><![CDATA[The Statue Of Liberty]]></name>
                </Attraction>
            </ListResults>
        </Destination>
 
    </ResultSet>';





$xml = simplexml_load_string($xmlcontent);

foreach ($xml->Destination->ListResults->Attraction as $attraction)
        {
            $name.=$attraction->name."<br/>";
        }

        echo "$name";
?>

Open in new window

0
Comment
Question by:Gosman
  • 2
3 Comments
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 250 total points
ID: 35215126
You need to make sure that you iterate the groups at the right level then iterate within each group

...... snipped!

$xml = simplexml_load_string($xmlcontent);

foreach ($xml->Destination as $attraction )
     foreach( $attraction->ListResults->Attraction as $aName )
            $name .= "{$aName->name}<br/>";
        

        echo "$name";
?>

Open in new window

0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 35215159
Another alternative is to use xpath

$xml = simplexml_load_string($xmlcontent);
$nodes = $xml->xpath('/ResultSet/Destination/ListResults/Attraction/name' );

foreach( $nodes as $aNode )
     echo "$aNode <br/>";

Open in new window


http://uk2.php.net/manual/en/simplexmlelement.xpath.php
0
 

Author Closing Comment

by:Gosman
ID: 35215332
Thanks Guys.

Both worked. I went with bportlock as best solution as it required less code and also highlighted xpath which I knew nothing about.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now