简体   繁体   English

用PHP解析大型嵌套XML工作表的最佳方法?

[英]Best way to parse a large, nested XML sheet with PHP?

I'm trying to parse a quite large XML-sheet with PHP, but I'm fairly new to it. 我正在尝试使用PHP解析一个很大的XML工作表,但是我对此并不陌生。 The XML-sheet contains a couple of thousands of records. XML工作表包含成千上万的记录。

Here is an example of the structure used within the sheet: 这是工作表中使用的结构的示例:

<familyList>
<family>
<familyID>1234</familyID>
<familyDescription>The Jonathans</familyDescription>
<childrenList>
<child>Suzan</child>
<child>Fred</child>
<child>Harry</child>
</childrenList>
</family>
<family>
<familyID>1235</familyID>
<familyDescription>The Gregories</familyDescription>
<childrenList>
<child>Anthony</child>
<child>Lindsay</child>
</childrenList>
</family>
</familyList>

As I'm fairly new to XML-parsing using PHP, I wonder what would be the best way to parse this nested XML-sheet into an array. 由于我刚开始使用PHP进行XML解析,因此我想知道将嵌套XML-sheet解析为数组的最佳方法是什么。 I need to convert the XML to an array so I can insert the data into a MySQL database afterwards. 我需要将XML转换为数组,以便以后可以将数据插入MySQL数据库。

Could you please give me a push in the right direction as I haven't been succesful puzzling out a solution sofar?.. 您能不能向我正确的方向提供帮助,因为我一直未能成功地解决一个问题?

Thanks! 谢谢!

When you are parsing large XML file, you should use a XML Pull Parser (XPP) to do so. 解析大型XML文件时,应使用XML Pull Parser(XPP)进行解析。 PHP has an implementation of a pull parser, it's called XMLReader . PHP具有拉式解析器的实现,称为XMLReader Also storing XML as an array for large file will consume a lot of memory. 将XML存储为大文件数组也会消耗大量内存。

What I recommend you is to use XMLReader and as you parse the data, you can insert it in your database without waiting for the end of the file. 我建议您使用XMLReader,并且在解析数据时,可以将其插入数据库中而无需等待文件结尾。 It won't use huge amount of memory and it will be faster. 它不会占用大量内存,而且速度更快。

This tutorial can be a good start to understand how to use XMLReader with PHP. 本教程可以成为了解如何将XMLReader与PHP结合使用的一个好的开始。

Has pointed out if the comments, XML Parser can be an other solution for parsing large XML file. 如果已指出注释,则XML Parser可以是解析大型XML文件的另一种解决方案。

DOMDocument has lots of excellent methods for accessing, updating and outputting formatted XML. DOMDocument具有许多用于访问,更新和输出格式化XML的出色方法。 With regards to converting to an array, I'd suggest going for objects inside an array , which is something that PHP is perfectly fine with, and I find the syntax much clearer than arrays for keeping track of this kind of hierarchy. 关于转换为数组,我建议在数组中使用对象 ,这是PHP完全可以解决的问题,并且我发现语法比数组更清晰,可以跟踪这种层次结构。

 <?php


// load xml families, could split this into different files..
$families = new DOMDocument();
$families->load("/xml/families.xml"); // your xml file

$families_items = $families->getElementsByTagName("family");

$my_cool_array = null;  // reset this variable for being set as an array literal later

foreach( $families_items as $family_item) {

    $toinsert = null; // reset the object literal

    $toinsert->family_id = $family_item->getElementsByTagName('familyID')->nodeValue;
    $toinsert->familyDescription= $family_item->getElementsByTagName('familyDescription')->nodeValue;

    $children = $family_item->getElementsByTagName('childrenList')->childNodes;


    // children
    foreach ($children as $child) {
        $child_toinsert[]->name = $child->nodeValue;
    }
    // etc for your details, syntax might be a bit off, but should get you started

    $toinsert->children = $child_toinsert;


    // build array of objects
    $my_cool_array_of_families[] = $toinsert;



}


var_dump($my_cool_array);

Something like this, double check the syntax, but it's on the way ;) 像这样的东西,请仔细检查语法,但是它正在路上;)

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

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