使用SimpleXMLElement创建OTA_HotelAvailRQ XML时节点层级错误求助
Fixing Incorrect Node Hierarchy in SimpleXMLElement for OTA_HotelAvailRQ
Let's break down what's going wrong with your code and how to fix it to generate the expected XML structure.
Key Issues in Your Current Code
- Wrong Node Nesting: You're adding
AvailRequestSegmentdirectly to the rootOTA_HotelAvailRQnode instead of nesting it insideAvailRequestSegments. This breaks the core hierarchy of your expected XML and likely contributes to the misplacedRoomStayCandidatesnode you're seeing. - Missing Child Nodes: Your code doesn't include the nested nodes like
RoomStayCandidate,GuestCounts, and the fullHotelSearchCriteriasub-structure that your expected XML requires. - Invalid Attribute Name: You're adding an attribute
123="321"toRoomStayCandidates—XML attribute names can't start with a number, and this isn't present in your desired output anyway.
Corrected Code
Here's the revised code that builds the exact structure you're expecting:
<?php // Define your variables first (replace with actual values) $Start = "2018-10-17+03:00"; $End = "2018-10-19+03:00"; $Country = "Attica"; // Example value for County // Initialize root node $xml = new SimpleXMLElement('<OTA_HotelAvailRQ></OTA_HotelAvailRQ>'); // Build AvailRequestSegments -> AvailRequestSegment hierarchy correctly $availRequestSegments = $xml->addChild("AvailRequestSegments"); $availRequestSegment = $availRequestSegments->addChild("AvailRequestSegment"); // Add StayDateRange with attributes $stayDateRange = $availRequestSegment->addChild("StayDateRange"); $stayDateRange->addAttribute("Duration", "P2N"); $stayDateRange->addAttribute("Start", $Start); $stayDateRange->addAttribute("End", $End); // Add RoomStayCandidates and its nested nodes $roomStayCandidates = $availRequestSegment->addChild("RoomStayCandidates"); $roomStayCandidate = $roomStayCandidates->addChild("RoomStayCandidate"); $roomStayCandidate->addAttribute("Quantity", "1"); $guestCounts = $roomStayCandidate->addChild("GuestCounts"); $guestCounts->addAttribute("IsPerRoom", "true"); $guestCount = $guestCounts->addChild("GuestCount"); $guestCount->addAttribute("Count", "2"); $guestCount->addAttribute("AgeQualifyingCode", "10"); // Add HotelSearchCriteria and its nested nodes $hotelSearchCriteria = $availRequestSegment->addChild("HotelSearchCriteria"); $criterion = $hotelSearchCriteria->addChild("Criterion"); $criterion->addAttribute("ExactMatch", "false"); $criterion->addChild("Position"); $address = $criterion->addChild("Address"); $address->addAttribute("FormattedInd", "true"); $address->addChild("CityName", "Athens Center"); $address->addChild("County", $Country); $countryName = $address->addChild("CountryName"); $countryName->addAttribute("Code", "GR"); // Output the XML echo $xml->asXML(); ?>
Why This Works
- Proper Nesting: We first create
AvailRequestSegmentsand store it in a variable, then addAvailRequestSegmentas a child of that node—this matches your expected hierarchy and ensures all subsequent nodes are placed under the correct parent. - Complete Node Structure: We build every nested node (like
RoomStayCandidate,GuestCounts, and theAddresssub-tree) exactly as shown in your desired XML. - Valid Attributes: All attributes follow XML naming rules and match the ones in your expected output.
When you run this code, it will generate the exact XML structure you outlined, with all nodes placed in their correct parent elements.
内容的提问来源于stack exchange,提问作者Abdul Rahman
相关产品推荐
相关产品推荐

