1

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

2

1 Answer 1

1

use LIBXML_PARSEHUGE

        $xml = new XMLReader();
        $xml->open($url, NULL, LIBXML_PARSEHUGE);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Bilal. it worked after adding LIBXML_PARSEHUGE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.