php xmlreader未返回完整数据

php xmlreader未返回完整数据,php,json,xmlreader,Php,Json,Xmlreader,您好,我正在尝试从XML文件获取所有数据,所以我使用了xmltoassoc函数。它适用于5 mb的文件,但不适用于超过9 mb的文件 这是我的密码: 我修改了这个函数来获取json代码 function xml2assoc($xml, $name) { $tree = null; while($xml->read()) { if($xml->nodeType == XMLReader::END_ELEMENT) {

您好,我正在尝试从XML文件获取所有数据,所以我使用了xmltoassoc函数。它适用于5 mb的文件,但不适用于超过9 mb的文件

这是我的密码: 我修改了这个函数来获取json代码

function xml2assoc($xml, $name)
{
    $tree = null;
    while($xml->read()) 
    {
        if($xml->nodeType == XMLReader::END_ELEMENT)
        {
            return $tree;
        }        
        else if($xml->nodeType == XMLReader::ELEMENT)
        {
            $node = array();            
            if($xml->hasAttributes)
            {
                $attributes = array();
                while($xml->moveToNextAttribute()) 
                {
                    $attributes[$xml->name] = $xml->value;
                }
            }

            if(!$xml->isEmptyElement)
            {               
                $childs = xml2assoc($xml, $node['tag']);
                if(isset($childs['text']))
                {
                   $tree = $childs;
                } else {
                   $tree['text'] = $childs[0];
                }
            }
        }        
        else if($xml->nodeType == XMLReader::TEXT)
        {   
            if(isset($xmlArr['text']))
            {
               $tree = $xmlArr;
            } else {
               $tree['text'] = $xmlArr[0];
            }            
        }
    }
    return $tree; 
}
我使用这个函数通过传递URL返回JSON

function PARSE_XML_JSON($url)
{
    $text = "";
    $xml = new XMLReader();
    $xml->open($url); 
    $assoc = xml2assoc($xml, "root"); 
    $xml->close();
    if(isset($assoc['text']))
    {
        $text = $assoc['text'];
    }
    //StoreInTxtFile($text);
    return $text;
}
我还尝试通过以下方式将数据保存到文件中:

function StoreInTxtFile($data)
{
    $myFile = 'jsonfile-'.time().'.txt';
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $data);
    fclose($fh);
}
请告诉我我错过了什么。 谢谢使用


参考这个链接:和这个:谢谢比拉尔。在添加
LIBXML\u
之后,它就工作了。
        $xml = new XMLReader();
        $xml->open($url, NULL, LIBXML_PARSEHUGE);