javax.xml.bind.UnmarshalException: XML document structures must start and end within the same entity

StackOverflow https://stackoverflow.com/questions/19965304

  •  30-07-2022
  •  | 
  •  

Question

I am trying to unmarshall an XML that I got as a BLAST output from NCBI. Unfortunately it seems to fail where it shouldn't. The stacktrace I get is:

org.xml.sax.SAXParseException; systemId: file:/C:/blast_output.xml; lineNumber: 578; columnNumber: 17; XML document structures must start and end within the same entity.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1436)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.endEntity(XMLDocumentFragmentScannerImpl.java:911)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.endEntity(XMLDocumentScannerImpl.java:563)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.endEntity(XMLEntityManager.java:1384)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1774)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipSpaces(XMLEntityScanner.java:1502)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.seekCloseOfStartTag(XMLDocumentFragmentScannerImpl.java:1386)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:246)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2778)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
    ... 37 more

The XML line referenced in stacktrace:

<Hsp_identity>8</Hsp_identity>

And the code for unmarshalling:

JAXBContext jc = JAXBContext.newInstance(BlastOutput.class);
Unmarshaller u = jc.createUnmarshaller();
return (BlastOutput) u.unmarshal(outputFile);

Maybe someone could explain what is failing here.

Was it helpful?

Solution

There may be a problem with your XML or a bug in the parser that is being used by JAXB in your configuration. You could try unmarshalling an instance of XMLStreamReader to force a different parser to be used.

import java.io.File;
import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        Demo demo = new Demo();

        XMLInputFactory xif = XMLInputFactory.newFactory();
        File outputFile = new File("input.xml");
        XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(outputFile));
        BlastOutput blastOutput = demo.unmarshalBlastOutput(xsr);
    }

    private BlastOutput unmarshalBlastOutput(XMLStreamReader xsr) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(BlastOutput.class);
        Unmarshaller u = jc.createUnmarshaller();
        return (BlastOutput) u.unmarshal(xsr);
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top