Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6166729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:23:14+00:00 2026-05-23T22:23:14+00:00

I am finding my way with XML parsers, and I try now XML Parser.

  • 0

I am finding my way with XML parsers, and I try now XML Parser. (I also testes XMLReader but I find it very slow).

As I read it has not any memory issues because it does not load the whole document in memory like DOM or SimpleXML.

I use this code for testing purposes replacing the working $data = fread($fp, 4096); with $data = fread($fp, filesize($file)); to load the whole document and display it, not only a small amount of it.

When I do this I get this error Fatal error: Allowed memory size of 67108864 bytes exhausted

Can anyone clear my mind and share with me some knowledge regarding this ?

The XML file is 120mb.

<?php
// The XML file that you wish to be parsed
$file = "standard.xml";

// This function tells the parser what to do with the data once it reaches the contents
// that appear between tags.
function contents($parser, $data){
    echo $data;
}

// This function tells the parser to place a <b> where it finds a start tag.
function startTag($parser, $data){
    echo "<b>";
}

// And this function tells the parser to replace the end tags with "<b><br />"
function endTag($parser, $data){
    echo "</b><br />";
}

// These lines create the parser and then set the functions for the parser to use when
// reading the document.
$xml_parser = xml_parser_create();

// Sets the functions for start and end tags
xml_set_element_handler($xml_parser, "startTag", "endTag");
// Sets the function for the contents/data
xml_set_character_data_handler($xml_parser, "contents");

// Opens the file for reading
$fp = fopen($file, "r");

// Read the file and save its contents as the variable "data"
$data = fread($fp, filesize($file));

// This if statement does two things. 1) it parses the document according to our 
// functions created above. 2) If the parse fails for some reason it returns an
// error message and also tells us which line the error occured at.
if(!(xml_parse($xml_parser, $data, feof($fp)))){
    die("Error on line " . xml_get_current_line_number($xml_parser));
}

// Free the memory used to create the parser
xml_parser_free($xml_parser);

// Close the file when you're done reading it
fclose($fp);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:23:15+00:00Added an answer on May 23, 2026 at 10:23 pm

    The line $data = fread($fp, filesize($file)); will read the whole file into memory. If you have 64MB of memory and the file is 120MB of size then i guess you can see why that fails.

    Reread the docs for xml_parse and feed it with smaller chunks for data to work within your memory limit 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Finding a good way to do this has stumped me for a while now:
Anybody has an alternate way of finding and copying files in bash than: find
I've been using jQuery .find() to do xml traversal, and I'm finding that occasionally
I'm trying to read in an XML configuration file, do a few tweaks (finding
I have trouble finding way to correctly refactor this code so that there would
I'm currently having difficulty finding a way of making my loops work. I have
Is there an easy way of finding out the host name of a machine
I am working on finding a good way to make user submitted data, in
What is a good way of finding the next and previous siblings of a
What is the easiest way of finding out what version of the iPhone SDK

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.