simplexml_load_string Acces multiple child nodes with the same name

I'm using simplexml_load_string to load and parse an XML file.

The xml file contains multiple child nodes with the same name. I'm using a foreach loop to step through the file, however when I try to parse the nodes with the same name only data from the the first node is returned.

Any help with how to return all values greatly appreciated.
<ValueAdds>
        <valueAdd id='2048'>Free Wireless Internet</valueAdd>
        <valueAdd id='2'>Continental Breakfast</valueAdd>
        <valueAdd id='128'>Free Parking</valueAdd>
</ValueAdds>

Open in new window

GosmanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shinesh PremrajanEngineering ManagerCommented:
Try with this loop

foreach($xml->valueAdd as $x=>$y)
echo "<br/> $x=>$y";

Hope this helps
0
shanikawmCommented:
try this
<?php
$xml= <<<XML
<ValueAdds>
        <valueAdd id='2048'>Free Wireless Internet</valueAdd>
        <valueAdd id='2'>Continental Breakfast</valueAdd>
        <valueAdd id='128'>Free Parking</valueAdd>
</ValueAdds>
XML;
$obj = simplexml_load_string($xml);
foreach($obj->valueAdd as $v)
{
 echo $v.PHP_EOL;
}
?>

Open in new window

0
Ray PaseurCommented:
You can use var_dump() to visualize the object made from SimpleXML_Load functions.  This will show you that "valueAdd" is an array.
http://www.laprbass.com/RAY_temp_gosman.php

Outputs:
object(SimpleXMLElement)#1 (1) {
  ["valueAdd"]=>
  array(3) {
    [0]=>
    string(22) "Free Wireless Internet"
    [1]=>
    string(21) "Continental Breakfast"
    [2]=>
    string(12) "Free Parking"
  }
}

2048 Free Wireless Internet
2 Continental Breakfast
128 Free Parking
<?php // RAY_temp_gosman.php
error_reporting(E_ALL);

// THE XML FROM THE POST AT EE
$xml= <<<ENDXML
<ValueAdds>
        <valueAdd id='2048'>Free Wireless Internet</valueAdd>
        <valueAdd id='2'>Continental Breakfast</valueAdd>
        <valueAdd id='128'>Free Parking</valueAdd>
</ValueAdds>
ENDXML;

// MAKE AN OBJECT
$obj = simplexml_load_string($xml);

// VISUALIZE THE OBJECT
echo "<pre>";
var_dump($obj);

// ITERATE OVER THE ARRAY
foreach ($obj->valueAdd as $val)
{
    echo PHP_EOL . $val["id"];
    echo " ";
    echo $val[0];
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GosmanAuthor Commented:
Hi Ray

I think the code above is in complete. I have looked at our example here http://www.laprbass.com/RAY_temp_gosman.php However I copied the code above and ran locally and I get the id's but not the names.

Result below.

object(SimpleXMLElement)#1 (1) {
  ["valueAdd"]=>
  array(3) {
    [0]=>
    string(22) "Free Wireless Internet"
    [1]=>
    string(21) "Continental Breakfast"
    [2]=>
    string(12) "Free Parking"
  }
}

2048
2
128
0
GosmanAuthor Commented:
Ray sorted it

Changed echo $val[0]; to echo $val;

All now working.


<?php // RAY_temp_gosman.php
error_reporting(E_ALL);

// THE XML FROM THE POST AT EE
$xml= <<<ENDXML
<ValueAdds>
        <valueAdd id='2048'>Free Wireless Internet</valueAdd>
        <valueAdd id='2'>Continental Breakfast</valueAdd>
        <valueAdd id='128'>Free Parking</valueAdd>
</ValueAdds>
ENDXML;

// MAKE AN OBJECT
$obj = simplexml_load_string($xml);

// VISUALIZE THE OBJECT
echo "<pre>";
var_dump($obj);

// ITERATE OVER THE ARRAY
foreach ($obj->valueAdd as $val)
{
    echo PHP_EOL . $val["id"];
    echo " ";
    echo $val;
}
?>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.