Remove <! DOCTYPE from property.storeToXML property output
Below is a sample of my code.
OutputStream outs = response.getOutputStream();
property.put("xyz", serverpath);
property.put("*abc", serverIPAddress);
property.storeToXML(outs, null, "UTF-8");
outs.close();
I don't need an ad DOCTYPE
. How do I remove it?
Current output:
Like most property classes, you cannot change it. Instead, take the resulting XML string, modify it, and then manually submit it.
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();
FYI ByteArrayOutputStream
is an in-memory output stream that you can use to capture and retrieve what was written in it. Since Properties
there won't be many records in the object in practice, this approach does not pose a risk of memory consumption.
Doctype is a header component, it doesn't matter for most purposes.
If you really want to remove it, you must write the result to a StringWriter or ByteArrayOutputStream and remove the unwanted content.
If you already have a line and want to delete it, you can use this
str.replaceAll("<!DOCTYPE((.|\n|\r)*?)\">", "");
Selected from here: http://www.gregbugaj.com/?p=270