java - org.xml.sax.SAXParseException;尝试从请求中获取 xml 输入时文件过早结束
问题描述
我正在尝试使用以下代码从请求中解析一个大 xml:
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();
}
doc = db.parse(is);
我在 where is中收到以下错误。我检查了eD
第一行代码的字符串,是空的。如何从 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)
当我尝试打印 eD 时,我得到了一个空字符串。
xml 输入的快照是:
<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>
解决方案
从:1:1:
错误输出中的位置标记,您解析了一个空字符串。
您的文档以第一行的第一个字符结尾,这可能是end of file
标记没有更多可用输入的模拟哨兵。
问题不在此代码中,而在于将字符放入eD
字符串的代码中。
由于已request.getReader()
填充eD
,您的请求很可能有问题。它要么返回一个404
,要么网络服务器返回一个300
系列重定向,或者其他一些通常有一个空正文的问题(假设它是 HTTP)。
在获取请求正文的内容之前检查请求的返回代码。它将告诉您需要知道什么才能找到解决此问题的正确答案。
推荐阅读
- python - Python 程序的输出为“无”
- c# - 我可以在字符串数组中获取某个用户的关注者吗
- c - 逐字符读取文件字符会导致最后一行截断
- intellij-idea - IntelliJ - 如何定义每个开发人员都有不同的 SDK 的项目 SDK?
- python - 计算三元图中数据点的面积和重叠面积?
- c++ - c ++互斥锁可以跨方法/变量吗?如果是这样,为什么它在这里不起作用?
- fosuserbundle - LogoutListener 给了我一个“InvalidArgumentException”
- python - Pandas:使用 [columnname][rownumber] 选择数组的值仅适用于多索引
- active-directory - Microsoft Active Directory 是否可以选择成为身份提供者?
- mongodb - 正确的 Mongoose 子文档关系