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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:20:03+00:00 2026-06-15T14:20:03+00:00

I am using STAX event API to read the binary data that is received

  • 0

I am using STAX event API to read the binary data that is received from a SOAP call and would like to stream the same to a consumer. The XML payload from the SOAP call is something like this:

    .........
    <BinaryObject mimeCode="text/xml">PHNvYXAtZW52OkVudmVsb3BlIHhtbG5zOnNvYXAtZW52PSJodHRwOi8vc
         2NoZW1hcy54bWxzb2FwLhm9yZy9zb2FwL2VudmVsb3BlLyI+DQogICA8c29hcC1lbnY6SGVhZGVy
         Lz4NCiAgIDxzb2FwLWVudjpCb2R5Pg0KICAgICAgPG5tOkF0dGFjaG1lbnRGb2xkZXJEb2N1bWVudE
         ZpbGVDb250ZW50QnlJRFJlc3BvbnNlX3N5bmMgeG1sbnM6bm09Imh0dHA6Ly9zYXAuY29tL3hpL1NB
         UEdsb2JhbDIwL0dsb2JhbCIgeG1sbnM6cHJ4PSJ1cm46c2FwLmNvbTpwcm94eTpISlc6LzFTQUkvVE
         FTMEIzNDE4MTJBNTc5MDUyM0I5RTU6ODA0Ij4NCiAgICAgICAgIDxBdHRhY..... </BinaryObject>

The following the java code that I use for parsing and sending the data to the consumer

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);

    InputStream in;

    try {

        in = new ByteArrayInputStream(response.getBytes());

        XMLEventReader eventReader;
        eventReader = inputFactory.createXMLEventReader(in);

        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();

            // Start element
            if (event.isStartElement()) {
                StartElement startElement = event.asStartElement();

                if (startElement.getName().getLocalPart().toString()
                        .equals("BinaryObject")) {

                    Iterator<Attribute> attributes = startElement
                            .getAttributes();

                    while (attributes.hasNext()) {
                        Attribute attribute = attributes.next();

                        if (attribute.getName().toString()
                                .equals("mimeCode")) {
                            mimeType = attribute.getValue();
                        }
                    }

                    event = eventReader.peek();

                    if (event.isCharacters()) {
                        event = eventReader.nextEvent();
                        content = event.asCharacters().getData();
                    }
                }
            }
        }

    } catch (XMLStreamException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    m_servletResponse.setContentType(mimeType);
        m_servletResponse.getWriter().print(javax.xml.bind.DatatypeConverter
                .printBase64Binary(content.getBytes()));

There are multiple issue with this code:

  1. For larger files (> 1 MB) I get a StackOverflow error

  2. Even for smaller files when I try with png files I get the error that the file is invalid (at the consumer).

How can I overcome these issues?

PS: Am using STAX for the first time !!

====================
EDIT:
====================**

Based on suggestion from Evgeniy below, I am now able to handle small files (e.g. PNG). However for large say PDF documents > 1 MB I get the error below. Any ideas as to what is going wrong here?

2012 12 09 06:50:19#+00#ERROR#System.err##anonymous#http-bio-8041-exec-9##seodportal#seodportal#web#null#null#Exception in thread “http-bio-8041-exec-9” |
2012 12 09 06:50:19#+00#ERROR#System.err##anonymous#http-bio-8041-exec-9##seodportal#seodportal#web#null#null#java.lang.StackOverflowError|
2012 12 09 06:50:19#+00#ERROR#System.err##anonymous#http-bio-8041-exec-9##seodportal#seodportal#web#null#null# at com.sun.org.apache.xerces.internal.impl.XMLScanner.isInvalid(XMLScanner.java:1334)|
2012 12 09 06:50:19#+00#ERROR#System.err##anonymous#http-bio-8041-exec-9##seodportal#seodportal#web#null#null# at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanCharReferenceValue(XMLScanner.java:1294)|
2012 12 09 06:50:19#+00#ERROR#System.err##anonymous#http-bio-8041-exec-9##seodportal#seodportal#web#null#null# at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3024)|
2012 12 09 06:50:19#+00#ERROR#System.err##anonymous#http-bio-8041-exec-9##seodportal#seodportal#web#null#null# at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2919)|
2012 12 09 06:50:19#+00#ERROR#System.err##anonymous#http-bio-8041-exec-9##seodportal#seodportal#web#null#null# at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3059)|

  • 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-06-15T14:20:04+00:00Added an answer on June 15, 2026 at 2:20 pm

    First of all, XMLEventReader is designed for special purposes, use XMLStreamReader instead. Here is a working example

            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
            InputStream in = new ByteArrayInputStream(response.getBytes());
            XMLStreamReader xr = inputFactory.createXMLStreamReader(in);
            while (xr.hasNext()) {
                int next = xr.next();
                if (next == XMLStreamConstants.START_ELEMENT) {
                    if (xr.getLocalName().equals("BinaryObject")) {
                        String mimeCode = xr.getAttributeValue(null, "mimeCode");
                        if (mimeCode.equals("text/xml")) {
                            xr.next();
                        // for efficiency we can access xr inner buffer chars directly
                        char[] b = xr.getTextCharacters();
                        int textStart = xr.getTextStart();
                        int textLength = xr.getTextLength();
                        // or simply get it as String  
                        String text = xr.getText();
                        // in this example I will use JDK's internal decoder com.sun.org.apache.xerces.internal.impl.dv.util.Base64                     
                        byte[] bytes = new Base64().decode(text);
    
                        }
                    }
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the StAX event based API's to modify an XML stream. The stream
I am using the StAX Streaming Api in vrsion 1.2.0 from http://stax.codehaus.org/ . When
I want to read DOM document using Stax stream readers and write it using
I'm trying to parse a processing instruction like this using StAX: <?item something=<some_element></some_element>?> StAX
I'm using XStream and JETTISON's Stax JSON serializer to send/receive messages to/from JSON javascripts
I am trying to write XML data using Stax where the content itself is
I am using woodstox to implement a StAX parser for XML files. Assume that
I have code to fetch characters from a StAX parser using eventReader. The code
Using Stax, I'm surprised to find that an XML block such as: <badger> <![CDATA[Text
We are using StAX parser to read the large XML files and strip off

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.