2016-09-02 3 views
0

Мне нужно сгенерировать этот xml с мылом, но я не могу понять, как создать вложенный xml с атрибутами.php вложенное мыло xml с атрибутами

Я могу сделать это

<ns1:SelectedSupplements> 
      <ns1:SupplementInfo suppId="16" supTotalPrice="0.00" suppType="4" /> 
      <ns1:SupplementInfo suppId="1000615" supTotalPrice="360.00" suppType="8" /> 
</ns1:SelectedSupplements> 

Но это не

<ns1:SelectedSupplements> 
      <ns1:SupplementInfo suppId="16" supTotalPrice="0.00" suppType="4" /> 
      <ns1:SupplementInfo suppId="1000615" supTotalPrice="360.00" suppType="8"> 
       <ns1:SupAgeGroup> 
        <ns1:SuppAges suppFrom="1" suppTo="7" suppQuantity="1" suppPrice="40.00"/> 
        <ns1:SuppAges suppFrom="8" suppTo="99" suppQuantity="2" suppPrice="80.00"/> 
       </ns1:SupAgeGroup> 
      </ns1:SupplementInfo> 
    </ns1:SelectedSupplements> 

Это мой код для первого XML

$room_class = new stdClass(); 
$supplement = array(); 

foreach($results["hotels"]["hotel"][0]["options"]["option"][$key]["fees"]["fee"] as $one_supp) 
{ 
    array_push($supplement, array("suppId"=> $one_supp["suppId"] , "supTotalPrice" => $one_supp["amt"] , "suppType" => $one_supp["supptType"])); 
} 
$room_class->SelectedSupplements = $supplement; 

ответ

-1

Вы можете использовать DOMDocument http://php.net/manual/en/class.domdocument.php для достижения этой цели ,

$doc = new DOMDocument(); 
$doc->formatOutput = true; 
$root = $doc->createElement('ns1:SelectedSupplements'); 
$root = $doc->appendChild($root); 

$supplementInfo = $doc->createElement('ns1:SupplementInfo'); 
$supplementInfo->setAttribute('suppId', 16); 
$supplementInfo->setAttribute('supTotalPrice', 0.00); 
$supplementInfo->setAttribute('suppType', 4); 

$supAgeGroup = $doc->createElement('ns1:SupAgeGroup'); 
$supAges = $doc->createElement('ns1:SuppAges'); 
$supAges->setAttribute('suppFrom', 1); 
$supAges->setAttribute('suppTo', 7); 
$supAges->setAttribute('suppQuantity', 1); 
$supAges->setAttribute('suppPrice', 40.00); 

$supAgeGroup->appendChild($supAges); 
$supplementInfo->appendChild($supAgeGroup); 


$root->appendChild($supplementInfo); 
$soapXml= $doc->saveXML($root); 

echo "<pre>"; 
print_r(htmlspecialchars($soapXml)); 
echo "</pre>" ; 
+0

Он создает вложенный XML-атрибут, но не создает дублирующие элементы SupplementInfo и SuppAges –

Смежные вопросы