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?
 
Ray PaseurConnect With a Mentor Commented:
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.