0

我一直在试图弄清楚如何打开一个 xml 文件并将其解析为一个数组。但我似乎无法让它正常工作。这是 XML 文件:

<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>

这是我的 php 代码(将其作为数组传递):

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;

    }

哪个有效,并给了我这个数组:

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
                                                )

                                        )

                                )

                        )

                )

        )

)

但是当我尝试遍历数组并回显信息时,它失败了。这是我尝试使用的代码:

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

但我得到的错误是:致命错误:在第 33 行的 default.php 中的非对象上调用成员函数属性()

有人可以帮我回显 XML 文件的详细信息吗?

4

2 回答 2

1

改成:

foreach($xmlArr->attributes() as $a => $b) {
    echo $a."=".$b."\n";
}
于 2013-10-31T03:19:56.793 回答
0

要获得所有孩子,您需要使用$xmlArr->children().

$xmlArr->attributes()返回当前元素的属性数组。

使用示例(在线演示):

<?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));
    }
}

程序输出:

Root Attributes:
 - Version = "1.0.0.0"

Root Children:
 - SoldMessage = "call"
 - RoomTypes = ""
   - RoomTypeId = "10"
   - SubPropertyId = "1"
   - Name = "K"
   - MaxOccupancy = "2"
   - Availability = ""
   - BookingRangeAvailable = "false"
   - ChargeTypes = ""
于 2013-10-31T03:34:54.153 回答