Shail Paras
Shail Paras

Reputation: 1163

php xmlreader not returning complete data

Hello I am trying to get all the data from XML File , so i have used xmltoassoc function. It is working for 5 mb file but not for more than 9mb file.

Here is my code: I have modified this function to get json code,

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; 
}

I have used this function to return JSON by passing URL.

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;
}

I have also tried to save data in files by doing this:

function StoreInTxtFile($data)
{
    $myFile = 'jsonfile-'.time().'.txt';
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $data);
    fclose($fh);
}

Please tell me what i'm missing. Thanks

Upvotes: 1

Views: 319

Answers (1)

NID
NID

Reputation: 3294

use LIBXML_PARSEHUGE

        $xml = new XMLReader();
        $xml->open($url, NULL, LIBXML_PARSEHUGE);

Upvotes: 1

Related Questions