简体   繁体   English

如何使用php处理XML文件?

[英]How do I process an XML file with php?

My gateway sends(posts) my server an xml datafeed when a purchase is made. 购买时,我的网关向服务器发送(发布)xml数据供稿。 The XML looks something like this: 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;

As a first time XML parsing experiencing, I was hoping somebody could show me how I would go about going through and turning this into a php array or something like that so I can then insert the data I want into a mysql table. 作为XML解析的第一次体验,我希望有人可以向我展示如何将其转换为php数组或类似的东西,以便随后将所需的数据插入mysql表。

Whats the easiest way to go about doing this? 最简单的方法是什么?

您可以使用PHP XML解析器,例如SimpleXML

You can use the following snippet with xml_parse_into_struct to get your XML into two PHP arrays, one with the data and another with pointers to where teach tag appears in the values array: 您可以将以下代码段与xml_parse_into_struct结合使用,以将XML分为两个PHP数组,一个包含数据,另一个包含指向教导标记在values数组中的位置的指针:

$parser = xml_parser_create();

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

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

var_dump($values);
var_dump($index)

$xml contains the XML data. $ xml包含XML数据。 Check the examples in the manual page I link to for further information. 查看我链接到的手册页中的示例以获取更多信息。

You can use the builtin XML parser , the manual should give you everything you need. 您可以使用内置的XML解析器 ,该手册应为您提供所需的一切。 But speaking from experience, I would really recommend learning Xpath , since it is much faster and much easier to use then anything else you can get with PHP. 但是从经验上来讲,我真的建议学习Xpath ,因为它使用PHP可以得到的任何其他东西都更快,更容易使用。 You could start with the introduction@w3schools , have a look at the nice examples in the specs and eventually use the PHP functions . 您可以从Introduction @ w3schools开始 ,看看规范中的漂亮示例 ,最后使用PHP函数

A sample query for selecting the transaction id would be 用于选择交易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);

Best is to use a PHP XML Parser. 最好是使用PHP XML解析器。 IMO, very best is to use PHP DOM because in every language (or so), you'll find this library, with the same methods. IMO,最好是使用PHP DOM,因为在每种语言中,您都可以使用相同的方法找到该库。

Parsing XML: 解析XML:
If your server receives just the XML ( not in $_POST['xmlfeed']) : 如果您的服务器仅接收XML(不在$ _POST ['xmlfeed']中):

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

Don't convert the SimpleXML object into an array. 不要将SimpleXML对象转换为数组。 It works pretty much like an array anyway. 无论如何,它的工作原理几乎就像一个数组。 You can use foreach and it's easy to access its elements. 您可以使用foreach,并且很容易访问其元素。
http://us2.php.net/manual/en/simplexml.examples-basic.php http://us2.php.net/manual/zh/simplexml.examples-basic.php

PS For large XML documents use XMLReader() instead of SimpleXML . PS对于大型XML文档,请使用XMLReader()而不是SimpleXML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM