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

  • Home
  • SEARCH
  • 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 7673223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:27:44+00:00 2026-05-31T16:27:44+00:00

I have a large XML file (many MBs) that I cannot afford to download

  • 0

I have a large XML file (many MBs) that I cannot afford to download as a whole.

<doc>
   <element>...</element>
   <element>...</element>
   ...
   <element>...</element>
</doc>

I need to read this XML from the web (or better – stream it) and then take the first N elements from its body to have them processed with an XSLT template.

What library/technique do you recommend for such a task? I am writing in Java.

Kindest regards,
Q.

  • 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-31T16:27:46+00:00Added an answer on May 31, 2026 at 4:27 pm

    You can use STAX to read the elements and redirect a subset of them to a different stream:

    final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    final XMLEventReader xmlReader = xmlInputFactory.createXMLEventReader(yourInputStream);
    final XMLEventWriter xmlWriter = xmlOutputFactory .createXMLEventWriter(yourOutputStream); //The place where the resulting partial XML will go
    
    while (xmlReader.hasNext()) {
        XMLEvent event = xmlReader.nextEvent();
    
        if (event ... some validation) {
            xmlWriter.add(event); //Forward it to xmlWriter
        }
    
        if (we have read enough elements) {
            break;
        }
    }
    xmlReader.close();
    xmlWriter.flush();
    xmlWriter.close();
    

    Addition:

    To write the closing tag, you should create a close tag event and pass it to same XMLEventWriter object whenever you need. For that you need an instance of XMLEventFactory. Here’s example of writing a closing tag:

    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent closeTagEvent = eventFactory.createEndElement("prefix", "http://namespace.com", "elementName");
    //This will create a closing tag event for a previously opened <prefix:elementName xmlns:prefix="http://namespace.com">
    
    xmlWriter.add(closeTagEvent);
    
    xmlWriter.flush();
    xmlWriter.close();
    

    P.S. Sorry for confusion with one of my comments suggesting to use XMLStreamWriter. I had never used XMLEventWriter so did not know how to use it properly until now.

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

Sidebar

Related Questions

I have a large xml file (1Gb). I need to make many queries on
I have a large xml file (40 Gb) that I need to split into
I have a very large XML file that I need to parse so I
Let's say I have to read from a directory that has many large XML
I have a large xml file that looks like this: 20120124 07:30:15.301, saving to
I have a request that returns a large xml file. I have the file
I have a large XML file that looks like <data> skdfnlsniisimsoinfsdfoisdfinsdofinodnfonf <emrosem> 23324097234097g </emrosem>
I have a function, that gets a large XML file, then parses it, and
I have a fairly large xml file ( greater than 2mb ) that I'm
So I have this pretty large XML file (40MB) that I'll have to repeatedly

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.