Solved

simplexml_load_string() Problem

Posted on 2011-03-25
3
406 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

948 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

20 Experts available now in Live!

Get 1:1 Help Now