无法正确获取XML结构

时间:2017-06-08 13:16:42

标签: php xml

我正在使用XML和php。我正在尝试使用两个<customer>客户获得这种XML格式的PHP结果数组,我可以获得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来获取两位客户的信息。

1 个答案:

答案 0 :(得分:1)

通过客户使用foreach循环:

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