使用php查找一个巨大的xml文件的结构


Finding the structure of a huge xml file with php

我有一个52GB的xml文件,我需要插入到数据库中,但我不知道结构。我一直在搜索如何用XMLReader迭代它,但似乎我必须知道这样做的结构。如果我执行next()不止一次,它就会转到文件的末尾,如果我只执行一次,它会给我所有数据所在的第一个节点,由于内存问题,我看不到任何东西。

    $reader = new XMLReader();
    $reader->open('D:'_WORK'ESStatistikListeModtag.xml');
    $reader->read();
    $reader->next();
    var_dump($reader->expand());

这就是我所尝试的,我尝试了XMLReader的不同功能,但没有成功。我该怎么做呢?谢谢你的帮助和建议。

不能立即采取两部分

这个简单的代码可以帮助你理解结构。将路径数组设置为空并在输出顶层查找,例如。将其设置在数组路径中并观看下一级和。如果你不喜欢公共交通:),把'car'写入数组,然后看下一关…

$xml = new XMLReader(); 
$xml->open(FILENAME);
$path = array('root', 'car'...); 
$pp = array();
$selected = 0;           // requied fragment
$l = 0;                  // current level
$level = count($path);   // level to watch
while ($xml->read()) {
     if ($xml->nodeType == XMLReader::ELEMENT) {
        // Element start
        if ($l < $level) array_push($pp, $xml->name);
        if (($l == ($level-1) || !$level) && $path == $pp) { echo implode(', ', $pp)."<br>"; $selected = 1; }
        if (($l == $level) && ($selected )) echo "&nbsp;&nbsp;&nbsp;".$xml->name."<br>";
        $l++;
     }
    else if($xml->nodeType == XMLReader::END_ELEMENT) {
        // Element end
        if ($selected && ($l == $level)) {
            $selected = 0; 
            // you may write die here if you dont wait a repeats of "path"
        }
        $l--;
        if ($l < $level) array_pop($pp);       
    }
 }