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:
-
For larger files (> 1 MB) I get a StackOverflow error
-
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)|
First of all,
XMLEventReaderis designed for special purposes, useXMLStreamReaderinstead. Here is a working example