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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:01:33+00:00 2026-05-24T13:01:33+00:00

I am receving an XML file as an input, whose size can vary from

  • 0

I am receving an XML file as an input, whose size can vary from a few KBs to a lot more. I am getting this file over a network. I need to extract a small number of nodes as per my use, so most of the document is pretty useless for me. I have no memory preferences, I just need speed.

Considering all this, I concluded :

  1. Not using DOM here (due to possible huge size of doc , no CRUD requirement, and source being network)

  2. No SAX as I only need to get a small subset of data.

  3. StaX can be a way to go, but I am not sure if it is the fastest way.

  4. JAXB came up as another option – but what sort of parser does it use ? I read it uses Xerces by default (which is what type – push or pull ?), although I can configure it for use with Stax or Woodstock as per this link

I am reading a lot, still confused with so many options ! Any help would be appreciated.

Thanks !

Edit : I want to add one more question here : What is wrong in using JAXB here ?

  • 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-24T13:01:34+00:00Added an answer on May 24, 2026 at 1:01 pm

    Fastest solution is by far a StAX parser, specially as you only need a specific subset of the XML file and you can easily ignore whatever isn’t really necessary using StAX, while you would receive the event anyway if you were using a SAX parser.

    But it’s also a little bit more complicated than using SAX or DOM. One of these days I had to write a StAX parser for the following XML:

    <?xml version="1.0"?>
    <table>
        <row>
            <column>1</column>
            <column>Nome</column>
            <column>Sobrenome</column>
            <column>email@gmail.com</column>
            <column></column>
            <column>2011-06-22 03:02:14.915</column>
            <column>2011-06-22 03:02:25.953</column>
            <column></column>
            <column></column>
        </row>
    </table>    
    

    Here’s how the final parser code looks like:

    public class Parser {
    
    private String[] files ;
    
    public Parser(String ... files) {
        this.files = files;
    }
    
    private List<Inscrito> process() {
    
        List<Inscrito> inscritos = new ArrayList<Inscrito>();
    
    
        for ( String file : files ) {
    
            XMLInputFactory factory = XMLInputFactory.newFactory();
    
            try {
    
                String content = StringEscapeUtils.unescapeXml( FileUtils.readFileToString( new File(file) ) );
    
                XMLStreamReader parser = factory.createXMLStreamReader( new ByteArrayInputStream( content.getBytes() ) );
    
                String currentTag = null;
                int columnCount = 0;
                Inscrito inscrito = null;           
    
                while ( parser.hasNext() ) {
    
                    int currentEvent = parser.next();
    
                    switch ( currentEvent ) {
                    case XMLStreamReader.START_ELEMENT: 
    
                        currentTag = parser.getLocalName();
    
                        if ( "row".equals( currentTag ) ) {
                            columnCount = 0;
                            inscrito = new Inscrito();                      
                        }
    
                        break;
                    case XMLStreamReader.END_ELEMENT:
    
                        currentTag = parser.getLocalName();
    
                        if ( "row".equals( currentTag ) ) {
                            inscritos.add( inscrito );
                        }
    
                        if ( "column".equals( currentTag ) ) {
                            columnCount++;
                        }                   
    
                        break;
                    case XMLStreamReader.CHARACTERS:
    
                        if ( "column".equals( currentTag ) ) {
    
                            String text = parser.getText().trim().replaceAll( "\n" , " "); 
    
                            switch( columnCount ) {
                            case 0:
                                inscrito.setId( Integer.valueOf( text ) );
                                break;
                            case 1:                         
                                inscrito.setFirstName( WordUtils.capitalizeFully( text ) );
                                break;
                            case 2:
                                inscrito.setLastName( WordUtils.capitalizeFully( text ) );
                                break;
                            case 3:
                                inscrito.setEmail( text );
                                break;
                            }
    
                        }
    
                        break;
                    }
    
                }
    
                parser.close();
    
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }           
    
        }
    
        Collections.sort(inscritos);
    
        return inscritos;
    
    }
    
    public Map<String,List<Inscrito>> parse() {
    
        List<Inscrito> inscritos = this.process();
    
        Map<String,List<Inscrito>> resultado = new LinkedHashMap<String, List<Inscrito>>();
    
        for ( Inscrito i : inscritos ) {
    
            List<Inscrito> lista = resultado.get( i.getInicial() );
    
            if ( lista == null ) {
                lista = new ArrayList<Inscrito>();
                resultado.put( i.getInicial(), lista );
            }
    
            lista.add( i );
    
        }
    
        return resultado;
    }
    
    }
    

    The code itself is in portuguese but it should be straightforward for you to understand what it is, here’s the repo on github.

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

Sidebar

Related Questions

We are receiving an XML file from our client. I want to load the
A little help needed. I'm receiving an xml file similar to this: <?xml version=1.0
I'm trying to import an xml file into vb.net XmlDocument but am getting the
I'm parsing an XML file which can contain localized strings in different languages (at
I am in need of using classic ASP to create an XML file from
My app is accessing data from a remote XML file. I have no issues
I am trying to pull text from a single element in an xml file,
I am trying to modify an xml file from my aspx code. The file
I'm receiving feedback from a developer that The only way visual basic (6) can
I am having some issues receiving UTF-8 XML files back from DHL API. As

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.