title: Creating XML Using SimpleXMLElement
body: <pre><code>&lt;?php
  $xml =new SimpleXMLElement('&lt;OTA_HotelAvailRQ&gt;&lt;/OTA_HotelAvailRQ&gt;');
  
  $xml-&gt;addChild("AvailRequestSegments");
      $AvailRequestSegment = $xml-&gt;addChild("AvailRequestSegment");
          $StayDateRange = $AvailRequestSegment-&gt;addChild("StayDateRange");
                  $StayDateRange-&gt;addAttribute("Duration", "P2N");
                  $StayDateRange-&gt;addAttribute("Start", $Start);
                  $StayDateRange-&gt;addAttribute("End", $End);
          $RoomStayCandidates = $AvailRequestSegment-&gt;addChild("RoomStayCandidates");
                  $RoomStayCandidates-&gt;addAttribute("123", "321");
  
          $HotelSearchCriteria = $AvailRequestSegment-&gt;addChild("HotelSearchCriteria");
  
  echo $xml-&gt;asXML();
  ?&gt;
  </code></pre>
  
  <p>The XML I need to generate is </p>
  
  <pre><code>&lt;OTA_HotelAvailRQ&gt;
     &lt;AvailRequestSegments&gt;
        &lt;AvailRequestSegment&gt;
           &lt;StayDateRange Duration="P2N" Start="2018-10-17+03:00" End="2018-10-19+03:00" /&gt;
           &lt;RoomStayCandidates&gt;
              &lt;RoomStayCandidate Quantity="1"&gt;
                 &lt;GuestCounts IsPerRoom="true"&gt;
                    &lt;GuestCount Count="2" AgeQualifyingCode="10" /&gt;
                 &lt;/GuestCounts&gt;
              &lt;/RoomStayCandidate&gt;
           &lt;/RoomStayCandidates&gt;
           &lt;HotelSearchCriteria&gt;
              &lt;Criterion ExactMatch="false"&gt;
                 &lt;Position /&gt;
                 &lt;Address FormattedInd="true"&gt;
                    &lt;CityName&gt;Athens Center&lt;/CityName&gt;
                    &lt;County&gt;'.$Country.'&lt;/County&gt;
                    &lt;CountryName Code="GR" /&gt;
                 &lt;/Address&gt;
              &lt;/Criterion&gt;
           &lt;/HotelSearchCriteria&gt;
        &lt;/AvailRequestSegment&gt;
     &lt;/AvailRequestSegments&gt;
  &lt;/OTA_HotelAvailRQ&gt;
  </code></pre>
  
  <p>But whenever I add a child [RoomStayCandidates ] node to <code>$AvailRequestSegment</code> it is been added as child to <code>$StayDateRange</code> .</p>
  
  <p>The XML generated for my code is 
  <a href="https://i.stack.imgur.com/tdZmB.png" rel="nofollow noreferrer">XML Generated for My code</a></p>
  
  <p>I think my code is correct. but i don't get the exact output.
  Please help in this regards.
  TIA</p>