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 986097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:15:57+00:00 2026-05-16T05:15:57+00:00

I need to parse several large size XML files (one is ~8GB, others are

  • 0

I need to parse several large size XML files (one is ~8GB, others are ~4MB each) and merge them. Since both SAX and Tie::File are not suitable due to memory and time issues, I decided to try Twig.

Suppose each XML file is composed of several elements as follows:

<class name=math>
     <student>luke1</student>
     ... (a very very long list of student)
   <student>luke8000000</student>
</class>
<class name=english>
   <student>mary1</student>
     ...
   <student>mary1000000</student>
</class>

As you see, even if I use TwigRoots => {"class[\@name='english']" => \&counter} I still need to wait a long time for Twig to start to parse class=english because it needs to go over each line of class=math first (correct me if it does not need to go over each line).

Is there any way to let Twig start the parsing from a line number, rather than the beginning of a file? I can get the line number of <class name = english> using grep, which is much faster.

Thanks in advance.

  • 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-16T05:15:58+00:00Added an answer on May 16, 2026 at 5:15 am

    Perhaps this example will give you some ideas for an alternative strategy. In particular, you might be able to combine the idea in index_file with Zoul’s suggestion about seeking to a location before passing off the file handle to XML::Twig.

    use strict;
    use warnings;
    
    # Index the XML file, storing start and end positions
    # for each class in the document. You pay this cost only once.
    sub index_file {
        local @ARGV = (shift);
        my (%index, $prev);
        while (<>){
            if ( /^<class name=(\w+)>/ ) {
                my $start = tell() - length();
                $index{$1} = { start => $start, end => undef };
    
                $index{$prev}{end} = $start - 1 if defined $prev;
                $prev = $1;
            }        
            $index{$prev}{end} = tell if eof;
        }
        return \%index;
    }
    
    # Use the index to retrieve the XML for a particular class.
    # This allows you to jump quickly to any section of interest.
    # It assumes that the sections of the XML document are small enough
    # to be held in memory.
    sub get_section {
        my ($file_name, $class_name, $index) = @_;
        my $ind = $index->{$class_name};
    
        open(my $fh, '<', $file_name) or die $!;    
        seek $fh, $ind->{start}, 0;
        read( $fh, my $xml_section, $ind->{end} - $ind->{start} );
    
        return $xml_section;
    }
    
    # Example usage.
    sub main {
        my ($file_name) = @_;
        my $index = index_file($file_name);
        for my $cn (keys %$index){
            # Process only sections of interest.
            next unless $cn eq 'math' or $cn eq 'english';
            my $xml = get_section($file_name, $cn, $index);
    
            # Pass off to XML::Twig or whatever.
            print $xml;
        }
    }
    
    main(@ARGV);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's an XML stream which I need to parse. Since I only need to
I have a series of large, flat text files that I need to parse
I need to parse invalid HTML files that contain several random elements (like BODY)
I need to parse a fairly large XML file (varying between about a hundred
I need to use XmlDocument or HAP to parse several files, what is the
I need to parse through a string and add single quotes around each Guid
I need to parse some simple binary Files. (The files contains n entries which
I have an application where I need to parse or tokenize XML and preserve
I need to parse a huge xml feed containing games data and download all
I need to parse files generated by a third-party application. Using ANTLR, I have

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.