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?
2 Answers 2
change to:
foreach($xmlArr->attributes() as $a => $b) {
echo $a."=".$b."\n";
}
-
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– SoulieBaby2013年10月31日 03:21:43 +00:00Commented Oct 31, 2013 at 3:21
-
1@SoulieBaby attributes() returns just the attributes from xmlSudhir Bastakoti– Sudhir Bastakoti2013年10月31日 03:23:09 +00:00Commented Oct 31, 2013 at 3:23
-
Ok wait I figured it out, there's only attributes from certain parameters.. Thanks for your help :)SoulieBaby– SoulieBaby2013年10月31日 03:23:26 +00:00Commented Oct 31, 2013 at 3:23
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 = ""
Explore related questions
See similar questions with these tags.
var_dump
andprint_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 :)