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?

3
  • 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 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 :) Commented Oct 31, 2013 at 7:57
  • 1
    possible duplicate of How to get values of xml elements? Commented Oct 31, 2013 at 8:01
  • 1
    Related comment: stackoverflow.com/questions/19689882/… Commented Oct 31, 2013 at 8:20

2 Answers 2

1

change to:

foreach($xmlArr->attributes() as $a => $b) {
    echo $a."=".$b."\n";
}
Sign up to request clarification or add additional context in comments.

3 Comments

That just returns "Version=1.0.0.0", I'm trying to get it to return all of the information from the XML file.
@SoulieBaby attributes() returns just the attributes from xml
Ok wait I figured it out, there's only attributes from certain parameters.. Thanks for your help :)
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 = ""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.