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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:31:15+00:00 2026-05-24T02:31:15+00:00

While developing an adapter for a webservice, I’ve ended up facing a response like

  • 0

While developing an adapter for a webservice, I’ve ended up facing a response like this:

<?xml version="1.0" encoding="UTF-8"?>
<ResponseHeader version="1.0">
    <ResponseCode>T100</ResponseCode>
    <SubmissionIdentifier>1</SubmissionIdentifier>
</ResponseHeader>

<?xml version="1.0" encoding="UTF-8"?>
<SubmissionProgress xmlns="sss"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        
    status="inProgress"
    submissionIdentifier="1"
    submissionType="live">
    <PFile status="rejected"
        index="1"
        pFileIdentifier="999">
        <Exception errorCode="2001" outcomeType="rejectFile">
            <Description>There.file.  </Description>
            <SourceRecord index="3">...</SourceRecord>
        </Exception>
    </PFile>
</SubmissionProgress>

ResponseHeader and SubmissionProgress (and every element inside) classes have been successfully generated by xjc and, if I split this string into 2 different string I can unmarshall both classes perfectly.
But, if I keep it in the same String and try to pass it to both unmarshallers sequentially it breaks in the first unmarshall.
I’m using this code to unmarshall both from one String:

Reader reader = new StringReader(response);
JAXBContext jcrh = JAXBContext.newInstance(ResponseHeader.class);
JAXBContext jcsp = JAXBContext.newInstance(SubmissionProgress.class);
Unmarshaller urh = jcrh.createUnmarshaller();
Unmarshaller usp = jcsp.createUnmarshaller();
ResponseHeader rh = (ResponseHeader) urh.unmarshal(reader);
SubmissionProgress sr = (SubmissionProgress) usp.unmarshal(reader);

And I get the following exception (at ResponseHeader rh = (ResponseHeader) urh.unmarshal(reader);):

uk.co.bacs.submissions.ResponseHeader@fced4
javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
(...)

Is there some JAXB tweak to use in these cases (multiple XML files in one single stream)?

  • 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-24T02:31:17+00:00Added an answer on May 24, 2026 at 2:31 am

    As there is no way for JAXB to read through the files by itself, I’ve found 2 working solutions.

    The first and simpler one, in case the stream is small, would be to read it all into one string and split it

    String xml = "<?xml ... <?xml ...";
    String[] xmlArray = xml.split("<\\?xml");
    ObjectA a = (ResponseHeader) u.unmarshal(new StringReader("<?xml"+xmlArray[1]);
    ObjectB b = (SubmissionProgress) u2.unmarshal(new StringReader("<?xml"+xmlArray[2));
    

    But, as an exercise, for cleaner code and future use with bigger streams (dealing with one object at a time), I made MultiXMLDocReader class

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.Reader;
    
    public class MultiXMLDocReader extends Reader {
        private BufferedReader reader;
        private String buffer;
        private int bufferPos;
        private boolean firstDocument;
        private boolean realEOF;
        private boolean enforceEOF;
    
        public MultiXMLDocReader(Reader reader) {
            this.reader = new BufferedReader(reader);
            firstDocument = true;
            buffer = "";
            bufferPos = 0;
            realEOF = enforceEOF = false;
        }
    
        @Override
        public void close() throws IOException {
            enforceEOF = false;
            if (realEOF) reader.close();
        }
    
        @Override
        public int read() throws IOException {
            char[] buffer = new char[1];
            int result = read(buffer, 0, 1);
            if (result < 0) return -1;
            return buffer[0];
        }
    
        @Override
        public int read(char[] cbuf, int off, int len) throws IOException {
            if (enforceEOF) return -1;
            int lenLeft = len;
            int read = 0;
            while (lenLeft > 0) {
                if (buffer.length()>0) {
                    char[] lbuffer = buffer.toCharArray();
                    int bufLen = buffer.length() - bufferPos;
                    int newBufferPos = 0;
                    if (lenLeft < bufLen) {
                        bufLen = lenLeft;
                        newBufferPos = bufferPos + bufLen;
                    }
                    else buffer = "";
                    System.arraycopy(lbuffer, bufferPos, cbuf, off, bufLen);
                    read += bufLen;
                    lenLeft -= bufLen;
                    off += bufLen;
                    bufferPos = newBufferPos;
                    continue;
                }
                buffer = reader.readLine();
                if (buffer == null) {
                    realEOF = true;
                    enforceEOF = true;
                    return (read == 0 ? -1 : read);
                }
                else
                    buffer += "\n";
                if (buffer.startsWith("<?xml")) {
                    if (firstDocument) firstDocument = false;
                    else {
                        enforceEOF = true;
                        return (read == 0 ? -1 : read);
                    }
                }
            }
            return read;
        }
    }
    

    which can be used as easily as

    MultiXMLDocReader xmlReader = new MultiXMLDocReader(new InputStreamReader(anyInputStream));
    ObjectA a = (ResponseHeader) u.unmarshal(xmlReader);
    ObjectB b = (SubmissionProgress) u2.unmarshal(xmlReader);
    

    without loading the whole stream to a string.

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

Sidebar

Related Questions

While developing a joomla plugin, if my plugin folder looks like so: /install.xml /mainPg.php
While developing a firefox extension, I create a wizard window from overlay.js using: this.wizard
while developing an application how to get current operating system version installed in mobile
While I'm developing Android-Apps I like to have a look at internal SDK implementations.
While developing a test case to understand serialization, I've run into what looks like
I am facing some issue while developing Video capturing application. 1) When I start
While developing with Firebug I keep getting this error. pages[x].css(z-index,x) is not a function
I am getting this error while developing a stored procedure Implicit conversion of varchar
I ran into this problem while developing in Objective-C for iOS, but this should
While developing a C++ application, I had to use a third-party library which produced

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.