2009-07-16 76 views
1

我的網關在購買時發送(發佈)我的服務器的xml數據傳輸。該XML看起來是這樣的:如何使用php處理XML文件?

<?xml version='1.0' standalone='yes'?> 
<foxydata> 
<datafeed_version>XML FoxyCart Version 0.6</datafeed_version> 
<transactions> 
    <transaction> 
     <id>616</id> 
     <transaction_date>2007-05-04 20:53:57</transaction_date> 
     <customer_id>122</customer_id> 
     <customer_first_name>Dirk</customer_first_name> 
     <customer_last_name>Gently</customer_last_name> 
     <shipping_total>4.38</shipping_total> 
     <order_total>24.38</order_total> 
     <order_total>24.38</order_total> 
       <customer_password>1aab23051b24582c5dc8e23fc595d505</customer_password> 
     <custom_fields> 
      <custom_field> 
       <custom_field_name>My_Cool_Text</custom_field_name> 
       <custom_field_value>Value123</custom_field_value> 
      </custom_field> 
     </custom_fields> 
     <transaction_details> 
      <transaction_detail> 
       <product_name>foo</product_name> 
       <product_price>20.00</product_price> 
       <product_quantity>1</product_quantity> 
       <product_weight>0.10</product_weight> 
       <product_code></product_code> 
       <subscription_frequency>1m</subscription_frequency> 
       <subscription_startdate>2007-07-07</subscription_startdate> 
       <next_transaction_date>2007-08-07</next_transaction_date> 
       <shipto>John Doe</shipto> 
       <category_description>Default for all products</category_description> 
       <category_code>DEFAULT</category_code> 
       <product_delivery_type>shipped</product_delivery_type> 
       <transaction_detail_options> 
        <transaction_detail_option> 
         <product_option_name>color</product_option_name> 
         <product_option_value>blue</product_option_value> 
         <price_mod></price_mod> 
         <weight_mod></weight_mod> 
        </transaction_detail_option> 
       </transaction_detail_options> 
      </transaction_detail> 
     </transaction_details> 
     <shipto_addresses> 
      <shipto_address> 
       <address_name>John Doe</address_name> 
       <shipto_first_name>John</shipto_first_name> 
       <shipto_last_name>Doe</shipto_last_name> 
       <shipto_address1>2345 Some Address</shipto_address1> 
       <shipto_address2></shipto_address2> 
       <shipto_city>Some City</shipto_city> 
       <shipto_state>TN</shipto_state> 
       <shipto_postal_code>37013</shipto_postal_code> 
       <shipto_country>US</shipto_country> 
       <shipto_shipping_service_description>DHL: Next Afternoon</shipto_shipping_service_description> 
       <shipto_subtotal>52.15</shipto_subtotal> 
       <shipto_tax_total>6.31</shipto_tax_total> 
       <shipto_shipping_total>15.76</shipto_shipping_total> 
       <shipto_total>74.22</shipto_total> 
       <shipto_custom_fields> 
        <shipto_custom_field> 
         <shipto_custom_field_name>My_Custom_Info</shipto_custom_field_name> 
         <shipto_custom_field_value>john's stuff</shipto_custom_field_value> 
        </shipto_custom_field> 
        <shipto_custom_field> 
         <shipto_custom_field_name>More_Custom_Info</shipto_custom_field_name> 
         <shipto_custom_field_value>more of john's stuff</shipto_custom_field_value> 
        </shipto_custom_field> 
       </shipto_custom_fields> 
      </shipto_address> 
     </shipto_addresses> 
    </transaction> 
</transactions> 
</foxydata> 
XML; 

作爲第一次XML解析經歷,我希望有人能告訴我,我怎麼會去經歷,並把這個變成一個PHP數組或類似的這樣我就可以什麼然後將我想要的數據插入到一個mysql表中。

要做到這一點最簡單的方法是什麼?

回答

2

你可以使用builtin XML parser,手冊應該給你你需要的一切。但從經驗來講,我真的推薦學習Xpath,因爲它是很多更快,更容易使用,那麼你可以用PHP獲得任何其他東西。你可以從[email protected]開始,看看nice examples in the specs並最終使用PHP functions

用於選擇交易ID查詢示例將

$document = new DOMDocument(); 
$document->load('YourFile.xml'); 
$xpath = new DOMXPath($document); 

# Query id 
$ids = $xpath->query('/transactions/transaction/id/text()'); 
# $ids is a DOMNodeList object 
assert($ids->length === 1); 
$id = $ids->item(0); 

# Query date 
$dates = $xpath->query('/transactions/transaction/transaction_date/text()'); 
# $dates is a DOMNodeList object 
assert($dates->length === 1); 
$id = $dates->item(0); 
+0

我建議你也學習XPath,但是之前的DOM,因爲它是XML =的基礎) – 2009-07-16 14:06:12

6

您可以使用PHP的XML解析器,像SimpleXML

+0

SimpleXML的+1,擊敗DOM易於使用的手。 – alastairs 2009-07-16 14:07:22

3

您可以使用下面的代碼片段,其中xml_parse_into_struct讓你的XML爲兩個PHP數組,一個與數據,另一個在何處教標籤出現在values array:

$parser = xml_parser_create(); 

$values = array(); 
$index = array(); 

xml_parse_into_struct ($parser, $xml, &$values, &$index); 

var_dump($values); 
var_dump($index) 

$ xml包含XML數據。查看我鏈接到的手冊頁中的示例以獲取更多信息。

1

最好是使用PHP的XML解析器。 國際海事組織,最好是使用PHP DOM,因爲在每種語言(或多或少)中,您都可以使用相同的方法找到此庫。

0

解析XML:
如果您的服務器接收只是XML(不屬於$ _ POST [ 'xmlfeed']):

$xml = file_get_contents("php://input"); 
$data = simplexml_load_string ($xml); 

不要轉換的SimpleXML對象到一個數組。無論如何,它的工作原理與數組非常相似。你可以使用foreach,並且很容易訪問它的元素。
http://us2.php.net/manual/en/simplexml.examples-basic.php

P.S.對於大型XML文檔,使用XMLReader()而不是SimpleXML。