Abdul Rahman
Abdul Rahman

Reputation: 2029

Creating XML Using SimpleXMLElement

<?php
$xml =new SimpleXMLElement('<OTA_HotelAvailRQ></OTA_HotelAvailRQ>');

$xml->addChild("AvailRequestSegments");
    $AvailRequestSegment = $xml->addChild("AvailRequestSegment");
        $StayDateRange = $AvailRequestSegment->addChild("StayDateRange");
                $StayDateRange->addAttribute("Duration", "P2N");
                $StayDateRange->addAttribute("Start", $Start);
                $StayDateRange->addAttribute("End", $End);
        $RoomStayCandidates = $AvailRequestSegment->addChild("RoomStayCandidates");
                $RoomStayCandidates->addAttribute("123", "321");

        $HotelSearchCriteria = $AvailRequestSegment->addChild("HotelSearchCriteria");

echo $xml->asXML();
?>

The XML I need to generate is

<OTA_HotelAvailRQ>
   <AvailRequestSegments>
      <AvailRequestSegment>
         <StayDateRange Duration="P2N" Start="2018-10-17+03:00" End="2018-10-19+03:00" />
         <RoomStayCandidates>
            <RoomStayCandidate Quantity="1">
               <GuestCounts IsPerRoom="true">
                  <GuestCount Count="2" AgeQualifyingCode="10" />
               </GuestCounts>
            </RoomStayCandidate>
         </RoomStayCandidates>
         <HotelSearchCriteria>
            <Criterion ExactMatch="false">
               <Position />
               <Address FormattedInd="true">
                  <CityName>Athens Center</CityName>
                  <County>'.$Country.'</County>
                  <CountryName Code="GR" />
               </Address>
            </Criterion>
         </HotelSearchCriteria>
      </AvailRequestSegment>
   </AvailRequestSegments>
</OTA_HotelAvailRQ>

But whenever I add a child [RoomStayCandidates ] node to $AvailRequestSegment it is been added as child to $StayDateRange .

The XML generated for my code is XML Generated for My code

I think my code is correct. but i don't get the exact output. Please help in this regards. TIA

Upvotes: 0

Views: 71

Answers (1)

Abdul Rahman
Abdul Rahman

Reputation: 2029

<?php
$xml =new SimpleXMLElement('<OTA_HotelAvailRQ></OTA_HotelAvailRQ>');

$xml->addChild("AvailRequestSegments");
    $AvailRequestSegment = $xml->addChild("AvailRequestSegment");
        $StayDateRange = $AvailRequestSegment->addChild("StayDateRange");
                $StayDateRange->addAttribute("Duration", "P2N");
                $StayDateRange->addAttribute("Start", $Start);
                $StayDateRange->addAttribute("End", $End);
        $RoomStayCandidates = $AvailRequestSegment->addChild("RoomStayCandidates");
                $RoomStayCandidates->addAttribute("123", "321");

        $HotelSearchCriteria = $AvailRequestSegment->addChild("HotelSearchCriteria");

echo $xml->asXML();
?>

This Worked. Thank You.

Upvotes: 0

Related Questions