?
Solved

simplexml_load_string() Problem

Posted on 2011-03-25
3
Medium Priority
?
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 1000 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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