理解Xml GET文件并制作有意义的数组

时间:2015-04-08 13:17:17

标签: php arrays xml

我必须将通过GET接收的此类文件转换为可在DB中编写的可管理数组。我对Xml知之甚少,虽然我试着学习,但它们仍然是象形文字。可以请有人帮帮我吗?这些文件并不完全类似于示例代码,并且通过节点或从这样的节点向下传递的某种Php灵活函数作为key =>值数组返回会很有趣。我在这个论坛上找到了array_map('read_xml', xpath('//node_to_be_parsed')),但返回的数组是空的,也许是

function read_xml(SimpleXMLElement $node_to_be_parsed)
{ 
    $array = array();
    foreach ($product->a_node as $some_node) 
        { $array[(string)$some_node["attr"]] = (string)$a_node; }
    return $array;
}

太简单了。任何帮助将非常感谢。谢谢你们。马丁

<envelope>
    <header>
        <version>1.5.2</version>
        <timestamp>20141030174828</timestamp>
    </header>
    <response type="Response type" product="songs">
        <search number="66637"/>
        <nights number="7"/>
        <checkin date="2014-11-15"/>
        <checkout date="2014-11-22"/>
        <songs>
            <song id="32" name="A name" vote="9" country="Country" city="City">
                <agreement id="Agreement Id 100" available="true" matched="true" c_type="7" room_basis="FB" meal_basis="A" rental_type="CC" checkindays="SAT" itinerary_id="91" itinerary_name="Itinerary Name" name="A title" min_nights="7" currency="EUR" deadline="2014-11-01" total="2257.92" total_compulsory_remarks="80.00" total_compulsory_on_place="300.00" special="true">
                    <remarks>
                        <remark text="Remark text ABC" id="689"></remark>
                        <remark text="Remark text DEF" id="690"></remark>
                        <remark text="Remark text GHI" id="309">
                            <structured applicable="pax" inadvance="true" toadd="true" compulsory="true">
                                <ammount calculated_child_value="0" child_value="25" type="EUR" calculated_value="80" value="40"/>
                            </structured>
                        </remark>
                        <remark text="Remark text XXX" id="2833">
                            <structured applicable="pax" inadvance="false" toadd="true" compulsory="true">
                                <ammount calculated_child_value="0" child_value="150" type="EUR" calculated_value="300" value="150"/>
                            </structured>
                        </remark>
                        <remark text="Remark text YYY" id="2835"></remark>
                        <remark text="Remark text ZZZ" id="2836">
                            <structured applicable="pax" inadvance="true" toadd="true" compulsory="false">
                                <ammount calculated_child_value="0" child_value="125" type="EUR" calculated_value="250" value="125"/>
                            </structured>
                        </remark>
                    </remarks>
                    <room type="Aaa" required="2">
                        <price from="2014-11-15" to="2014-11-22">
                            <roomprice nett="1128.96" child="1128.96" />
                        </price>
                    </room>
                </agreement>
                <agreement id="Agreement Id 200" available="true" matched="true" c_type="16" room_basis="FB" meal_basis="A" rental_type="CC" checkindays="SAT" itinerary_id="91" itinerary_name="Itinerary name" name="A name" min_nights="7" currency="EUR" deadline="2014-11-01" total="3517.92" total_compulsory_remarks="80.00" total_compulsory_on_place="0.00">
                    <remarks>
                        <remark text="................." id="689"></remark>
                            <structured applicable="......................">
                                <ammount calculated_child_value="0" child_value="250" type="USD" calculated_value="800" value="350"/>
                            </structured>
                    </remarks>
                    <room type="Bbb" required="2">
                        <price from="......." to=".">
                            <roomprice nett="......" child="......." />
                        </price>
                    </room>
                </agreement>
            </song>
        </songs>
    </response>
</envelope>

0 个答案:

没有答案