0

I've been trying to figure out how to open an xml file and parse it as an array. But I can't seem to get it working properly. Here's the XML file:

<RMSAvailRateChartRS Version="1.0.0.0">
  <SoldMessage>call</SoldMessage>
  <RoomTypes>
    <RoomType>
      <RoomTypeId>10</RoomTypeId>
      <SubPropertyId>1</SubPropertyId>
      <Name>K</Name>
      <MaxOccupancy>2</MaxOccupancy>
      <Availability Id="1" Date="2013-11-04" Available="false" NoOfRoomsAvailable="0" />
      <BookingRangeAvailable>false</BookingRangeAvailable>
      <ChargeTypes>
        <ChargeType>
          <ChargeTypeId>8</ChargeTypeId>
          <Name>BAR</Name>
          <Description>Best Rate Available</Description>
          <Charge Id="1" Date="2013-11-04" Price="100.00" MinStay="1" MaxStay="25" ValidCharge="true" IncludeChildInBase="false" IncludeInfantInBase="false" Flames="false" CanArriveToday="True" PersonBase="2" ChildBase="0" InfantBase="0" />
        </ChargeType>
      </ChargeTypes>
    </RoomType>
  </RoomTypes>
</RMSAvailRateChartRS>

Here's my php code (to pass it as an array):

public static function getXML($id) {

        $file = JPATH_SITE.'/tmp/request-'.$id.'.xml';
        if (file_exists($file)) :
            $xml = simplexml_load_file($file,'SimpleXMLElement',LIBXML_NOCDATA);            
            return $xml;
        else :
            exit('Failed to open '.$file.'.');
        endif;

    }

Which works, and gives me this array:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [Version] => 1.0.0.0
        )

    [SoldMessage] => call
    [RoomTypes] => SimpleXMLElement Object
        (
            [RoomType] => SimpleXMLElement Object
                (
                    [RoomTypeId] => 10
                    [SubPropertyId] => 1
                    [Name] => K
                    [MaxOccupancy] => 2
                    [Availability] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [Id] => 1
                                    [Date] => 2013-11-04
                                    [Available] => false
                                    [NoOfRoomsAvailable] => 0
                                )

                        )

                    [BookingRangeAvailable] => false
                    [ChargeTypes] => SimpleXMLElement Object
                        (
                            [ChargeType] => SimpleXMLElement Object
                                (
                                    [ChargeTypeId] => 8
                                    [Name] => BAR
                                    [Description] => Best Rate Available
                                    [Charge] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [Id] => 1
                                                    [Date] => 2013-11-04
                                                    [Price] => 100.00
                                                    [MinStay] => 1
                                                    [MaxStay] => 25
                                                    [ValidCharge] => true
                                                    [IncludeChildInBase] => false
                                                    [IncludeInfantInBase] => false
                                                    [Flames] => false
                                                    [CanArriveToday] => True
                                                    [PersonBase] => 2
                                                    [ChildBase] => 0
                                                    [InfantBase] => 0
                                                )

                                        )

                                )

                        )

                )

        )

)

But when I try to run through the array and echo out the information, it fails. Here's the code I'm trying to use for that:

foreach($xmlArr->RMSAvailRateChartRS[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
};

But the error I'm getting is: Fatal error: Call to a member function attributes() on a non-object in default.php on line 33

Could someone please help me echoing out the details of the XML file?

hakre
  • 193,403
  • 52
  • 435
  • 836
SoulieBaby
  • 5,405
  • 25
  • 95
  • 145
  • 1
    It is not an array, it is an object. SimpleXML is sort of misleading here with `var_dump` and `print_r`, I suggest you to read through http://php.net/simplexml.examples-basic for generic usage of SimpleXML. For concrete questions about simplexml in PHP you can also search within the tag simplexml it has some good and common questions answered, most likely anything you'll ever need with it :) – hakre Oct 31 '13 at 07:57
  • 1
    possible duplicate of [How to get values of xml elements?](http://stackoverflow.com/questions/2849678/how-to-get-values-of-xml-elements) – hakre Oct 31 '13 at 08:01
  • 1
    Related comment: http://stackoverflow.com/questions/19689882/reflect-associate-array-in-browser#comment29251384_19689882 – hakre Oct 31 '13 at 08:20

2 Answers2

1

change to:

foreach($xmlArr->attributes() as $a => $b) {
    echo $a."=".$b."\n";
}
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0

To get all the children you'll need to use $xmlArr->children().

$xmlArr->attributes() returns an array of the attributes of the current element.

Usage Example (Online Demo):

<?php
/**
 * Parsing an XML file to a PHP array
 *
 * @link http://stackoverflow.com/q/19697845/367456
 */

$buffer = <<<BUFFER
<RMSAvailRateChartRS Version="1.0.0.0">
  <SoldMessage>call</SoldMessage>
  <RoomTypes>
    <RoomType>
      <RoomTypeId>10</RoomTypeId>
      <SubPropertyId>1</SubPropertyId>
      <Name>K</Name>
      <MaxOccupancy>2</MaxOccupancy>
      <Availability Id="1" Date="2013-11-04" Available="false" NoOfRoomsAvailable="0" />
      <BookingRangeAvailable>false</BookingRangeAvailable>
      <ChargeTypes>
        <ChargeType>
          <ChargeTypeId>8</ChargeTypeId>
          <Name>BAR</Name>
          <Description>Best Rate Available</Description>
          <Charge Id="1" Date="2013-11-04" Price="100.00" MinStay="1" MaxStay="25" ValidCharge="true" IncludeChildInBase="false" IncludeInfantInBase="false" Flames="false" CanArriveToday="True" PersonBase="2" ChildBase="0" InfantBase="0" />
        </ChargeType>
      </ChargeTypes>
    </RoomType>
  </RoomTypes>
</RMSAvailRateChartRS>
BUFFER;

$xml = simplexml_load_string($buffer);

echo "Root Attributes:\n";
foreach ($xml->attributes() as $name => $value) {
    printf(" - %s = \"%s\"\n", $name, $value);
};

echo "\nRoot Children:\n";
foreach ($xml->children() as $name => $value) {
    printf(" - %s = \"%s\"\n", $name, trim($value));

    if ($name != 'RoomTypes') {
        continue;
    }

    foreach ($value->RoomType->children() as $childName => $childValue) {
        printf("   - %s = \"%s\"\n", $childName, trim($childValue));
    }
}

Program Output:

Root Attributes:
 - Version = "1.0.0.0"

Root Children:
 - SoldMessage = "call"
 - RoomTypes = ""
   - RoomTypeId = "10"
   - SubPropertyId = "1"
   - Name = "K"
   - MaxOccupancy = "2"
   - Availability = ""
   - BookingRangeAvailable = "false"
   - ChargeTypes = ""
hakre
  • 193,403
  • 52
  • 435
  • 836
alexg
  • 902
  • 11
  • 37