2017-06-08 82 views
0

我正在使用XML和PHP。我試圖將兩個<customer>客戶的PHP結果數組轉換爲這種XML格式,我可以得到1,但不知道如何爲數組中的2個客戶完成此操作。無法獲得XML結構

<?xml version="1.0"?> 
<NewSLSCase> 
    <Identification CientNo="4xxx" CLientBatchNo="1" IJBatchNo="1" HubNo="201048" ClientEmailAddress1="[email protected]" ClientEmailAddress2="[email protected]"/> 
    <LedgerSet> 
    <Ledger ProductionUnit="04" LedgerNo="4xxxxx"> 
     <CustomerSet> 
     <Customer No="123456" Name="Farhana" Address1="Myhouse" ZipCode="40100" City="Vantaa" LanguageCode="FIN" CountryCode="FI" VatNo="01011-111A" TypeCode="C"/> 
     <Customer No="123457" Name="Asiakas Anna" Address1="Asiakastie 2" ZipCode="00100" City=" Helsinki" LanguageCode="FIN" CountryCode="FI" VATNo="010111-111A" TypeCode="C"/> 
     </CustomerSet> 
     <InvoiceSet> 
     <Invoice InvoiceNo="123456"/> 
     <InvoiceHeader InvoiceHeaderType="L" InvoiceCustomerNo="123456" InvoiceCurrency="EUR" InvoiceDate="2011-04-08" InvoiceDueDate="2011-05-15" InvoiceAmount="57.00" ClientOCRReference="908000101800031186" InvoiceReferenceText3="150,00" InvoiceReferenceText4="Laskuille tulostuva" InvoiceReferenceText5="IBAN" InvoiceReferenceText6="BIC"/> 
     </InvoiceSet> 
    </Ledger> 
    </LedgerSet> 
</NewSLSCase> 

我寫了剛剛得到一個``但添加另一個代碼是我真的不確定關閉。

這裏是陣列,從大的角度看並沒有複製整個事物,但我使用它的一小部分進行測試。

array(2) (
    [0] => stdClass object { 
    ID => (string) 177835 
    currency => (string) Asiakas Anna 
    type => (string) Asiakastie 2 
    } 
    [1] => stdClass object { 
    ID => (string) 177840 
    Name=> (string) Farhana 
    Address=> (string) Myhouse 
    ..... 

這裏是整個XML格式

$result = $this->invoice_page->getInvoiceByID('20241'); //this is the array 


     $xml = new DomDocument('1.0'); 
     $xml->formatOutput = true; 
     $NewSLSCase = $xml->createElement("NewSLSCase"); 
     $xml->appendChild($NewSLSCase); 

     $Identification = $xml->createElement("Identification"); 
     $Identification->setAttribute("CientNo","4xxx"); 
     $Identification->setAttribute("CLientBatchNo", "1"); 
     $Identification->setAttribute("CLientBatchNo", "1"); 
     $Identification->setAttribute("IJBatchNo", "1"); 
     $Identification->setAttribute("HubNo", "201048"); 
     $Identification->setAttribute("ClientEmailAddress1", "[email protected]"); 
     $Identification->setAttribute("ClientEmailAddress2", "[email protected]"); 
     $NewSLSCase->appendChild($Identification); 

     $LedgerSet = $xml->createElement("LedgerSet"); 
     $NewSLSCase->appendChild($LedgerSet); 

     $Ledger = $xml->createElement("Ledger"); 
     $Ledger->setAttribute("ProductionUnit","04"); 
     $Ledger->setAttribute("LedgerNo", "4xxxxx"); 
     $LedgerSet->appendChild($Ledger); 

     $CustomerSet = $xml->createElement("CustomerSet"); 
     $Ledger->appendChild($CustomerSet); 

     $Customer = $xml->createElement("Customer"); 
     $Customer->setAttribute("No","123456"); 
     $Customer->setAttribute("Name", $result['Fullname']); 
     $Customer->setAttribute("Address1", $result['Address_street']); 
     $Customer->setAttribute("ZipCode", $result['Address_zip']); 
     $Customer->setAttribute("City",$result['Address_city']); 
     $Customer->setAttribute("LanguageCode", "FIN"); 
     $Customer->setAttribute("CountryCode","FI"); 
     $Customer->setAttribute("VatNo", "01011-111A"); 
     $Customer->setAttribute("TypeCode","C"); 
     $CustomerSet->appendChild($Customer); 


     $InvoiceSet = $xml->createElement("InvoiceSet"); 
     $Ledger->appendChild($InvoiceSet); 

     $Invoice = $xml->createElement("Invoice"); 
     $Invoice->setAttribute("InvoiceNo","123456"); 
     $InvoiceSet->appendChild($Invoice); 

     $InvoiceHeader = $xml->createElement("InvoiceHeader"); 
     $InvoiceHeader->setAttribute("InvoiceHeaderType", "L"); 
     $InvoiceHeader->setAttribute("InvoiceCustomerNo","123456"); 
     $InvoiceHeader->setAttribute("InvoiceCurrency", "EUR"); 
     $InvoiceHeader->setAttribute("InvoiceDate","2011-04-08"); 
     $InvoiceHeader->setAttribute("InvoiceDueDate", "2011-05-15"); 
     $InvoiceHeader->setAttribute("InvoiceAmount","57.00"); 
     $InvoiceHeader->setAttribute("ClientOCRReference", "908000101800031186"); 
     $InvoiceHeader->setAttribute("InvoiceReferenceText3","150,00"); 
     $InvoiceHeader->setAttribute("InvoiceReferenceText4","Laskuille tulostuva"); 
     $InvoiceHeader->setAttribute("InvoiceReferenceText5",$result['IBAN']); 
     $InvoiceHeader->setAttribute("InvoiceReferenceText6",$result['BIC']); 
     $InvoiceSet->appendChild($InvoiceHeader); 


     echo "<xmp>".$xml->saveXML()."</xmp>"; 

什麼上面的代碼,讓我爲這個

<?xml version="1.0"?> 
<NewSLSCase> 
    <Identification CientNo="4xxx" CLientBatchNo="1" IJBatchNo="1" HubNo="201048" ClientEmailAddress1="[email protected]" ClientEmailAddress2="[email protected]"/> 
    <LedgerSet> 
    <Ledger ProductionUnit="04" LedgerNo="4xxxxx"> 
     <CustomerSet> 
     <Customer No="123456" Name="Farhana" Address1="Myhouse" ZipCode="40100" City="Vantaa" LanguageCode="FIN" CountryCode="FI" VatNo="01011-111A" TypeCode="C"/> 
     </CustomerSet> 
     <InvoiceSet> 
     <Invoice InvoiceNo="123456"/> 
     <InvoiceHeader InvoiceHeaderType="L" InvoiceCustomerNo="123456" InvoiceCurrency="EUR" InvoiceDate="2011-04-08" InvoiceDueDate="2011-05-15" InvoiceAmount="57.00" ClientOCRReference="908000101800031186" InvoiceReferenceText3="150,00" InvoiceReferenceText4="Laskuille tulostuva" InvoiceReferenceText5="IBAN" InvoiceReferenceText6="BIC"/> 
     </InvoiceSet> 
    </Ledger> 
    </LedgerSet> 
</NewSLSCase> 

我不知道如何添加foreach獲得兩個客戶信息。

回答

1

通過使用客戶foreach循環:

foreach ($customersArray as $customerData) { 
    $Customer = $xml->createElement("Customer"); 
    $Customer->setAttribute("No",$customerData->ID); 
    //set all attributes like that 
    $CustomerSet->appendChild($Customer); 
} 
+0

謝謝!有用!!! – FaF