从 property.storeToXML 的输出中删除 <!DOCTYPE

ami*_*ngh 5 java xml

以下是我的代码示例。

OutputStream outs = response.getOutputStream();
property.put("xyz", serverpath);
property.put("*abc", serverIPAddress);

property.storeToXML(outs, null, "UTF-8");           
outs.close();
Run Code Online (Sandbox Code Playgroud)

我不需要DOCTYPE声明。如何去除它?

电流输出为:

在此输入图像描述

Boh*_*ian 4

与大多数 Properties 类一样,您无法更改它。相反,捕获生成的 XML 字符串,对其进行修改,然后手动将其发送出去。

property.put("xyz", "serverpath");
property.put("*abc", "serverIPAddress");
ByteArrayOutputStream out = new ByteArrayOutputStream();
property.storeToXML(out, null, "UTF-8");
String str = out.toString("UTF-8").replaceAll("<!DOCTYPE[^>]*>\n", "");
byte[] bytes = str.getBytes("UTF-8");
OutputStream outs = response.getOutputStream();
outs.write(bytes, 0, bytes.length);
outs.close();
Run Code Online (Sandbox Code Playgroud)

FYIByteArrayOutputStream是一个内存输出流,您可以使用它来捕获和检索写入的内容。由于Properties对象实际上不会有很多条目,因此这种方法不会带来内存消耗风险。