This is the response getting from a server. I am trying get value of an atribute Folio ID. How can we get using PHP

Viewed 17

I am getting below response from server and want extract attribute value from it. When I am using simplexml_load_string and DOMDocument it gives only PersonName & HKStatus outout.

XML

<OTA_ResRetrieveRS EchoToken="a" TimeStamp="2022-02-10T18:49:29+05:30" Version="0.0" Target="Test" xmlns="http://www.example.com/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/OTA/2003/05 OTA_ResRetrieveRS.xsd">
  <Success/>
  <ReservationsList>
   <HotelReservation ResStatus="In-house" RoomStayReservation="true" WalkInIndicator="true" CreatorID="roommaster:17.10">
    <RoomStays>
     <RoomStay MarketCode="UNK" SourceOfBusiness="WALK">
      <RoomTypes>
       <RoomType IsRoom="true" RoomTypeCode="KNG" RoomID="100"/>
      </RoomTypes>
      <RatePlans>
       <RatePlan RatePlanCode="RACK"/>
      </RatePlans>
      <GuestCounts>
       <GuestCount AgeQualifyingCode="10" Count="1"/>
      </GuestCounts>
      <TimeSpan Start="2022-02-10" End="2022-02-14"/>
     </RoomStay>
    </RoomStays>
    <ResGuests>
     <ResGuest PrimaryIndicator="true">
      <Profiles>
       <ProfileInfo>
        <UniqueID Type="10" ID="100000" ID_Context="roommaster"/>
        <Profile>
         <Customer>
          <PersonName>
          <NamePrefix>Mr</NamePrefix>
          <GivenName>Sanya</GivenName>
          <Surname>Raj</Surname>
          </PersonName>
         </Customer>
        </Profile>
       </ProfileInfo>
      </Profiles>
     </ResGuest>
    </ResGuests>
    <ResGlobalInfo>
     <Guarantee GuaranteeCode="CASH">
      <GuaranteesAccepted>
       <GuaranteeAccepted GuaranteeIndicator="true" PaymentTransactionTypeCode="CASH" GuaranteeTypeCode="1"/>
      </GuaranteesAccepted>
     </Guarantee>
     <HotelReservationIDs>
      <HotelReservationID ResID_Type="10" ResID_Value="100000-A" ResID_Source="roommaster" ResID_SourceContext="Master Folio"/>
     </HotelReservationIDs>
    </ResGlobalInfo>
    <TPA_Extensions>
     <HTNG>
      <ProfileMessageSummary Text="0"/>
     <HKStatus>Occupied</HKStatus>
     </HTNG>
     <Folios>
      <Folio ID="100000-A" Description="Master Folio" Balance="0"/>
     </Folios>
    </TPA_Extensions>
   </HotelReservation>
  </ReservationsList>
 </OTA_ResRetrieveRS>

PHP

$bcdoc = new DOMDocument();
$bcdoc->loadXML($xml);
$warning = $bcdoc->getElementsByTagName('Warnings')->item(0);
        
if(empty($warning)){
    $FolioID = $bcdoc->getElementsByTagName('Folio')->item(0)->nodeValue;
    echo $FoliaID."\n";
            
}else{
    $warning = $bcdoc->getElementsByTagName('Warnings')->item(0)->nodeValue;
    echo $warning."\n";
}

Only PersonName and HKstatus get output. I have used simplexml also to extract atribute but same result. What is the function can be used to extract attributes? Any idea how can I get value of Folio ID? In above example it is 100000-A.

1 Answers

The function you are looking for is getAttribute, but because getElementsByTagName returns an array, you will either need to get the first item or loop through the list if you have multiple folios.

$bcdoc = new DOMDocument();
$bcdoc->loadXML($xml);
$warning = $bcdoc->getElementsByTagName('Warnings')->item(0);

if (empty($warning)) {
  $FolioID  = $bcdoc->getElementsByTagName('Folio')[0]->getAttribute('ID');
  echo $FolioID . "\n";
} else {
  $warning = $bcdoc->getElementsByTagName('Warnings')->item(0)->nodeValue;
  echo $warning . "\n";
}

Related