Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

simplexml_load_string() Problem

Posted on 2011-03-25
3
Medium Priority
?
423 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 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