在我的变压器中,我有这样的配置:
trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "MLP_SVC_RESULT_320.DTD [<!ENTITY % extension SYSTEM \"company_mlp320_slia_extension.dtd\"> %extension;]");
这给了我一个错误:
[Fatal Error] :1:121: The document type declaration for root element type "svc_result" must end with '>'.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: xxx; The document type declaration for root element type "svc_result" must end with '>'.
因为它增加了一个额外的
"
在我的DOCTYPE行的末尾:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svc_result SYSTEM "MLP_SVC_RESULT_320.DTD [<!ENTITY % extension SYSTEM "company_mlp320_slia_extension.dtd"> %extension;]">
注意最后一个
"
,它不可能存在;正确的XML不包含它。
但我不知道如何在正确的配置下删除它。
一个正确的XML示例:
<?xml version="1.0" ?>
<!DOCTYPE svc_result SYSTEM "MLP_SVC_RESULT_320.DTD" [<!ENTITY % extension SYSTEM "company_mlp320_slia_extension.dtd"> %extension;]>
<svc_result ver="3.2.0">
<slia ver="3.0.0">
<pos>
<msid type="MSISDN" enc="ASC">200853000105614</msid>
<gsm_net_param>
<cgi>
<mcc>100</mcc>
<mnc>01</mnc>
<lac>2222</lac>
<cellid>10002</cellid>
</cgi>
<neid>
<vmscid>
<vmscno>00004946000</vmscno>
</vmscid>
<vlrid>
<vlrno>99994946000</vlrno>
</vlrid>
</neid>
</gsm_net_param>
</pos>
<company_mlp320_slia>
<company_netinfo>
<company_ms_netinfo type="CGI">
<msid type="MSISDN" enc="ASC">200853000105614</msid>
<time utc_off="+0200">20210521170211</time>
</company_ms_netinfo>
</company_netinfo>
</company_mlp320_slia>
</slia>
</svc_result>
完整代码:
private String transformToString(Document document) {
if (document == null) {
return null;
}
document.setXmlStandalone(true); // make document to be standalone, so we can avoid outputing standalone="no" in first line
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans;
try {
trans = tf.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "no"); // no extra indent; file already has intent of 4
trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "MLP_SVC_RESULT_320.DTD [<!ENTITY % extension SYSTEM \"company_mlp320_slia_extension.dtd\"> %extension;]");
StringWriter sw = new StringWriter();
trans.transform(new DOMSource(document), new StreamResult(sw));
// Spaces between tags are considered as text node, so when outputing we need to remove the extra empty lines
return sw.toString().replaceAll("\\n\\s*\\n", "\n");
} catch (TransformerException e) {
LOG.error("Cannot transform response document to String. ", e);
return null;
}
}