簡體   English   中英

無法正確獲取XML結構

[英]Cannot get XML structure right

我正在使用XML和php。 我正在嘗試用兩個<customer>客戶將PHP結果數組轉換為XML格式,我只能得到1個,但不知道如何為2個客戶在一個數組中這樣做。

<?xml version="1.0"?>
<NewSLSCase>
  <Identification CientNo="4xxx" CLientBatchNo="1" IJBatchNo="1" HubNo="201048" ClientEmailAddress1="etunimi.sukunimi@yritys.fi" ClientEmailAddress2="etunimi.sukunimi@yritys.fi"/>
  <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", "etunimi.sukunimi@yritys.fi");
        $Identification->setAttribute("ClientEmailAddress2", "etunimi.sukunimi@yritys.fi");
        $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="etunimi.sukunimi@yritys.fi" ClientEmailAddress2="etunimi.sukunimi@yritys.fi"/>
  <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以獲得兩個客戶信息。

通過客戶使用foreach循環:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM