Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-18-2009, 12:14 PM   #16
Registered: Oct 2004
Posts: 98

Original Poster
Rep: Reputation: 15

I should mention that my perl script to dump the data looks something like this. Note that I'm just using example data but the hash of arrays structure is the same.

use strict;
use XML::Simple;
use Data::Dumper;

my %arr_hash = ();
$arr_hash{'produce'}{'veggies'}[0] = "Broccoli";
$arr_hash{'produce'}{'veggies'}[1] = "Cauliflower";
$arr_hash{'produce'}{'veggies'}[2] = "Carrots";

my $xsimple = XML::Simple->new();

# Write XML File:
open(XMLPTR, ">sample.xml") or die "Cannot create xml file.\n";
print XMLPTR $xsimple->XMLout(\%arr_hash,
                       noattr => 1,
                       xmldecl => '<?xml version="1.0"?>');
This is what I'll change and fix so that maybe I can get a better xml file in the end. I've got pretty decent at perl so I think even if I had to manually create the file and not use XML::Simple I could still do it.
Old 02-19-2009, 02:33 PM   #17
Registered: Oct 2004
Posts: 98

Original Poster
Rep: Reputation: 15
Thumbs up Resolved: How do I work with large XML files in PHP?

Ok, I've resolved the problem. The reason SAX wasn't working was because there were ampersands (&) in the XML file and so I adjusted my perl script to use %26 instead. In fact, I've adjusted the code entirely so that now the php code reads this:


$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($xml_parser, "start_element", "end_element");
xml_set_character_data_handler($xml_parser, "characters");

$file = "sample.xml";
if ($file_stream = fopen($file, "r")) {

   while ($data = fread($file_stream, 4096)) {

       $this_chunk_parsed = xml_parse($xml_parser, $data, feof($file_stream));
       if (!$this_chunk_parsed) {
           $error_code = xml_get_error_code($xml_parser);
           $error_text = xml_error_string($error_code);
           $error_line = xml_get_current_line_number($xml_parser);

           $output_text = "Parsing problem at line $error_line: $error_text";


} else {

    die("Can't open XML file.");


// Functions

function start_element($parser, $name, $attrs) {
    print "<b>Start Element:</b> $name<br />";
    print "<b>---Attributes:</b> <br />";
    foreach ($attrs as $key => $value) {
        print "$key = $value<br />";
    print "<br />";

function end_element($parser, $name) {
    print "<b>End Element:</b> $name<br /><br />";

function characters($parser, $chars) {
		/* Parse % codes to normal HTML:
		   %26   &
		   %3F   ?
		   %3D   =
		   %2B   +
		   %2F   /
		$chars = str_replace(array("%26"), "&amp;", $chars);
		$chars = str_replace(array("%3F"), "?", $chars);
		$chars = str_replace(array("%3D"), "=", $chars);
		$chars = str_replace(array("%2B"), "+", $chars);
		$chars = str_replace(array("%2F"), "/", $chars);
    print "<p><i>$chars</i></p>";

Now it will work (note the changes in function characters).

This will work with a very large php file. But I did manage to cut it down to 11.6MB by adjusting the format. It won't work if I read it in all at once but using SAX it does work.


file, php, xml

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
ext3 performance -- very large number of files, large filesystems, etc. td3201 Linux - Server 5 11-25-2008 10:28 AM
getting xml to work with php/apache sholah Linux - Server 1 11-13-2008 12:41 AM
LXer: How to convert PDF files to HTML or XML files in openSUSE LXer Syndicated Linux News 0 08-20-2008 09:40 AM
LXer: The smart way to Process XML files with PHP LXer Syndicated Linux News 0 11-10-2007 01:30 AM
Splitting A Large Xml File anirudh Programming 7 09-03-2004 05:28 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:30 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration