简体   繁体   中英

org.xml.sax.SAXParseException; Premature end of file when trying to get an xml input from request

I'm trying to parse a big xml from request using the following code:

String eD = IOUtils.toString(request.getReader());
DocumentBuilder db = null;
    try {
        db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (ParserConfigurationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(eD));
    
    Document doc = null;
    try {
        doc = db.parse(is);
    } catch (SAXException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

I get the following error in line where doc = db.parse(is);is. I checked the eD string of the first line of code and is empty. How can I get the input from HttpServletRequest?

[Fatal Error] :1:1: Premature end of file.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)

When I'm trying to print eD I got an empty string.

A snapshot of the xml input is:

 <ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:epsos="urn:epsos-org:ep:medication" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <typeId extension="POCD_HD000040" root="2.16.840.1.113883.1.3"/>
    <templateId root="1.3.6.1.4.1.12559.11.10.1.3.1.1.2"/>
    <templateId root="1.3.6.1.4.1.19376.1.5.3.1.1.1"/>
    <id extension="5LMLbecMdsVlpoSA" root="2.16.840.1.113883.2.25.3.4.1.4.1"/>
    <code code="60593-1" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" codeSystemVersion="2.59" displayName="Medication dispensed.extended Document">
        <translation code="60593-1" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="eDispensation"/>
    </code>
<title>Medication dispensed</title>
<effectiveTime value="20201109112323+0200"/>
<confidentialityCode code="N" codeSystem="2.16.840.1.113883.5.25" codeSystemName="Confidentiality" codeSystemVersion="913-20091020" displayName="normal"/>
<languageCode code="el-GR"/>
<setId nullFlavor="NI"/>
<recordTarget>
    <patientRole>
        <id extension="1990-01-01_1" root="2.16.17.710.860.1000.990.1"/>
        <addr>
            <country>CY</country>
            <streetAddressLine>21, Dimokritou, 1000, Strovolos, Nicosia</streetAddressLine>
        </addr>
        <telecom use="H" value="tel:+99000000"/>
        <telecom use="H" value="mailto:l.petr@testmail.com"/>
        <patient>
            <name>
                <given>Leonidas</given>
                <family>Petrou</family>
            </name>
            <administrativeGenderCode code="M" codeSystem="2.16.840.1.113883.5.1" codeSystemName="AdministrativeGender" codeSystemVersion="913-20091020" displayName="Male"/>
            <birthTime value="19900101"/>
            <languageCommunication>
                <templateId root="1.3.6.1.4.1.19376.1.5.3.1.2.1"/>
                <languageCode code="en-GB"/>
            </languageCommunication>
        </patient>
    </patientRole>
</recordTarget>
<author typeCode="AUT">
    <functionCode code="2262" codeSystem="2.16.840.1.113883.2.9.6.2.7" codeSystemName="ISCO" codeSystemVersion="2008" displayName="Pharmacists"/>
    <time value="20201109112323+0200"/>
    
</author>
<inFulfillmentOf>
    <order moodCode="RQO">
        <id extension="ePid.1006.1.cda" root="2.16.196.1.101.10.3.29.54290"/>
    </order>
</inFulfillmentOf>
<relatedDocument typeCode="APND">
    <parentDocument classCode="DOCCLIN">
        <id extension="ePid.1006.1.cda" root="2.16.196.1.101.10.3.29.54290"/>
    </parentDocument>
</relatedDocument>
<component>
    <structuredBody>
        <component>
            <section>
                <templateId root="2.16.840.1.113883.10.20.1.8"/>
                <templateId root="1.3.6.1.4.1.12559.11.10.1.3.1.2.2"/>
                <id extension="D-ePid.1006.1.cda" root="2.16.840.1.113883.2.25.3.4.1.4.1"/>
                <code code="60590-7" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" codeSystemVersion="2.59" displayName="Medication dispensed.brief Document">
                    <translation code="60590-7" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="Medication dispensed"/>
                </code>
                <title>Dispensation: D-ePid.1006.1.cda</title>
                <text>
                    <table width="100%">
                        <thead>
                            <tr>
                                <th>Active Ingredient (ATC)</th>
                                <th>Pharmaceutical Dose Form</th>
                                <th>Strength</th>
                                <th>Number of Units per intake</th>
                                <th>Frequency of Intake</th>
                                <th>Duration of treatment</th>
                                <th>Number of Prescribed Packages</th>
                                <th>Size of the package prescribed and package type</th>
                                <th>Route of Administration</th>
                                <th>Substitution</th>
                                <th>Onset Date of treatment</th>
                                <th>Instructions to patient</th>
                                <th>Advise to the dispenser</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr ID="medicine.1">
                                <td>teriparatide (ATC: H05AA02)</td>
                                <td>Solution for injection</td>
                                <td>20 ug</td>
                                <td>1</td>
                                <td>1 per day</td>
                                <td>60 days</td>
                                <td>1</td>
                                <td>2 x 28 ug , Pre-filled pen</td>
                                <td>Subcutaneous use</td>
                                <td>Generic</td>
                                <td>2020-11-02</td>
                                <td>NA</td>
                                <td>NA</td>
                            </tr>
                        </tbody>
                    </table>
                </text>
            </section>
        </component>
    </structuredBody>
</component>
</ClinicalDocument>

From the :1:1: position marker in your error output, you parsed an empty string.

Your document is ending on the first character of the first line, which is probably the simulated end of file sentinel that marks no more input is available.

The problem is not within this code, it is within the code that puts characters into the eD string.

Since the request.getReader() populated eD , odds are you have an issue with your request. Either it returned a 404 or the web server returned a 300 series redirect, or some other issue (assuming it is HTTP) that would normally have an empty body.

Inspect the return code of your request before grabbing the contents of its body. It will tell you what you need to know to seek the right answers to fixing this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM