2016-09-02 6 views
0

Ich muss diese XML mit Seife generieren, aber ich kann nicht herausfinden, eine verschachtelte XML mit Attributen zu erstellen.php verschachtelte Seife xml mit Attributen

Ich bin in der Lage dieses

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

zu tun, aber nicht diese

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

Dies ist mein Code für das erste 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; 

Antwort

-1

können Sie verwenden DOMDocument http://php.net/manual/en/class.domdocument.php dies zu tun .

$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

Es erstellt verschachtelte XML mit Attributen, aber es erstellt nicht die doppelten Elemente SupplementInfo und SuppAges –