Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple XML "files" in one stream

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)?

like image 257
Filipe Pina Avatar asked Dec 21 '25 05:12

Filipe Pina


1 Answers

I don't know of a JAXB tweak; the way I've done this kind of thing is to implement an XmlEventReader (or XmlStreamReader) that simulates end-of-document when needed. Note that Unmarshaller.unmarshal() will take one of these as an argument. To make sure you get the event sequence right, watch a "normal" document's event sequence. You'll do two unmarshal()s.

like image 94
Ed Staub Avatar answered Dec 23 '25 19:12

Ed Staub