You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. Wrong Node Nesting: You're adding AvailRequestSegment directly to the root OTA_HotelAvailRQ node instead of nesting it inside AvailRequestSegments. This breaks the core hierarchy of your expected XML and likely contributes to the misplaced RoomStayCandidates node you're seeing.
  2. Missing Child Nodes: Your code doesn't include the nested nodes like RoomStayCandidate, GuestCounts, and the full HotelSearchCriteria sub-structure that your expected XML requires.
  3. Invalid Attribute Name: You're adding an attribute 123="321" to RoomStayCandidates—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 AvailRequestSegments and store it in a variable, then add AvailRequestSegment as 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 the Address sub-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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:28:57