2016-02-12 22 views
1

無效元素名稱這是我的xml:的DOMDocument :: loadXML的():開始標記:在實體

<?xml version="1.0"?> 
<data> 
    <client_name>Awesome Client</client_name> 
    <account_number/> 
    <date_created>02/12/2016</date_created> 
    <form_number>4126</form_number> 
    <customer_po/> 
    <terms_name>Credit Card</terms_name> 
    <date_shipped>12/31/1969</date_shipped> 
    <billing_contact_email/> 
    <billing_contact_address_line_1/> 
    <billing_contact_address_line_2/> 
    <billing_contact_address_line_3/> 
    <billing_contact_address_line_4/> 
    <billing_contact_address_city/> 
    <billing_contact_address_state>British Columbia</billing_contact_address_state> 
    <billing_contact_address_postal/> 
    <billing_contact_address_country>Canada</billing_contact_address_country> 
    <shipping_contact_address_line_1/> 
    <shipping_contact_address_line_2/> 
    <shipping_contact_address_line_3/> 
    <shipping_contact_address_line_4/> 
    <shipping_contact_address_city/> 
    <shipping_contact_address_state>British Columbia</shipping_contact_address_state> 
    <shipping_contact_address_postal/> 
    <shipping_contact_address_country>Canada</shipping_contact_address_country> 
    <billing_contact_first_name>another</billing_contact_first_name> 
    <billing_contact_last_name>client</billing_contact_last_name> 
    <client_rep_full_name>Rob Montebelli</client_rep_full_name> 
    <order_rep_full_name>Mark Graham</order_rep_full_name> 
    <job_name>77777</job_name> 
    <job_number>2620</job_number> 
    <event_type>Donor Gift</event_type> 
    <due_date>02/12/2016</due_date> 
    <shipping_method/> 
    <currency>CAD</currency> 
    <total_taxes>0.00</total_taxes> 
    <total_subtotal>1,760.16</total_subtotal> 
    <total>1,760.16</total> 
    <items> 
     <item0> 
      <taxes> 
       <0>E</0> 
      </taxes> 
      <title>1889-24</title> 
      <quantity>6</quantity> 
      <description>Carhartt (R) Signature Utility Duffel; TBD TBD</description> 
      <unit_price>159.32</unit_price> 
     </item0> 
     <item1> 
      <taxes> 
       <0>E</0> 
      </taxes> 
      <title>0022-56</title> 
      <quantity>12</quantity> 
      <description>Zoom (TM) DayTripper Sling Compu-Messenger; TBD TBD</description> 
      <unit_price>67.02</unit_price> 
     </item1> 
    </items> 
</data> 

我的代碼:

$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>'); 
array_to_xml($invoice, $xml_data); 
$xml = $xml_data->asXML(); 

$domxml = new DOMDocument('1.0'); 
$domxml->preserveWhiteSpace = false; 
$domxml->formatOutput = true; 
$domxml->loadXML($xml); 
$xml_string = $domxml->saveXML(); 

我認爲XML是正確的格式,爲什麼這是失敗的嗎?

編輯:

下面是使用XMLSimpleElement動態陣列之前被轉換成XML,反正是有除去< 0>之前被轉換成XML ?:

Array 
(
    [invoices] => Array 
     (
      [0] => Array 
       (
        [client_name] => Awesome Client 
        [account_number] => 
        [date_created] => 02/11/2016 
        [form_number] => 4104 
        [customer_po] => 
        [terms_name] => Credit Card 
        [date_shipped] => 12/31/1969 
        [billing_contact_email] => 
        [billing_contact_address_line_1] => 
        [billing_contact_address_line_2] => 
        [billing_contact_address_line_3] => 
        [billing_contact_address_line_4] => 
        [billing_contact_address_city] => 
        [billing_contact_address_state] => British Columbia 
        [billing_contact_address_postal] => 
        [billing_contact_address_country] => Canada 
        [shipping_contact_address_line_1] => 
        [shipping_contact_address_line_2] => 
        [shipping_contact_address_line_3] => 
        [shipping_contact_address_line_4] => 
        [shipping_contact_address_city] => 
        [shipping_contact_address_state] => British Columbia 
        [shipping_contact_address_postal] => 
        [shipping_contact_address_country] => Canada 
        [billing_contact_first_name] => another 
        [billing_contact_last_name] => client 
        [client_rep_full_name] => Rob Montebelli 
        [order_rep_full_name] => Mark Graham 
        [job_name] => 5010 
        [job_number] => 2598 
        [event_type] => Donor Gift 
        [due_date] => 02/11/2016 
        [shipping_method] => 
        [currency] => CAD 
        [total_taxes] => 0.00 
        [total_subtotal] => 1,760.16 
        [total] => 1,760.16 
        [items] => Array 
         (
          [0] => Array 
           (
            [taxes] => Array 
             (
              [0] => E 
             ) 

            [title] => 1889-24 
            [quantity] => 6 
            [description] => Carhartt (R) Signature Utility Duffel; TBD TBD 
            [unit_price] => 159.32 
           ) 

          [1] => Array 
           (
            [taxes] => Array 
             (
              [0] => E 
             ) 

            [title] => 0022-56 
            [quantity] => 12 
            [description] => Zoom (TM) DayTripper Sling Compu-Messenger; TBD TBD 
            [unit_price] => 67.02 
           ) 

         ) 

       ) 

     ) 

) 
+3

的''<0>是拋出它關閉,這是一個無效的元素名稱。 – chris85

+0

@ chris85哦我明白了,是否有使用函數刪除它,或者我應該將其從數組中刪除它,然後將其轉換爲xml? – Blkc

+0

刪除元素或警告? – chris85

回答

2

正如意見中提到的問題是與你的許多元素,<0>。這是一個無效的元素名稱。如果這是一個靜態文件,則可以手動修改XML文件,刪除或重命名,使用正則表達式或抑制錯誤。如果你壓抑,你將不得不記住,在將來,它永遠不會告訴你有關錯誤。

正則表達式的方法:

$invoice = preg_replace('~<(/?\d)~', '<number$1', $invoice); 

正則表達式演示:https://regex101.com/r/pZ3sJ4/1

一個抑制選項:

libxml_use_internal_errors(true); 
$domxml->loadXML($invoice); 
libxml_clear_errors(); 

通過這種方法,你仍然可以很容易地檢索錯誤。

例如

libxml_use_internal_errors(true); 
$domxml->loadXML($invoice); 
$xml_string = $domxml->saveXML(); 
$errors = libxml_get_errors(); 
foreach ($errors as $error) { 
    echo $error->message; 
} 
libxml_clear_errors(); 

第二抑制選項:

@$domxml->loadXML($invoice); 

呸..手動

三簡單的非編程選項,只需編輯文件:

<item1> 
      <taxes><name0>E</name0></taxes> 
      <title>0022-56</title> 
      <quantity>12</quantity> 
      <description>Zoom (TM) DayTripper Sling Compu-Messenger; TBD TBD</description> 
      <unit_price>67.02</unit_price> 
     </item1> 
+0

我應用了正則表達式$ XML,但我有此錯誤:'DOM文檔:: loadXML的():打開和結束標記不匹配:在Entity' – Blkc

+0

線產品數2和稅收問題是結束標記沒有與替換< /所以我用了2個正則表達式,而不是1.再次感謝。 – Blkc

+0

這個答案是偉大的,如果你有超過原始的XML無法控制。如果是自己生成XML,答案是沒有裂傷的結果,而是解決您的發電機擺在首位,以**創建有效的XML **。 – IMSoP

相關問題