Solved

simplexml_load_string() Problem

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

743 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

15 Experts available now in Live!

Get 1:1 Help Now