Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove <!DOCTYPE from output of property.storeToXML

Tags:

java

xml

Following are the 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 require DOCTYPE declaration. How to remove it?

Current output is:

enter image description here

like image 753
Amit Singh Avatar asked May 11 '15 09:05

Amit Singh


2 Answers

Like most of the Properties class, you can't change it. Instead, capture the XML string produced, modify it, then send it out manually.

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 you can use to capture and retrieve what was written to it. Because a Properties object is in practice not going to have many entries, this approach does not pose a memory consumption risk.

like image 55
Bohemian Avatar answered Sep 22 '22 02:09

Bohemian


If you already have string and want to remove it then you can make use of this

str.replaceAll("<!DOCTYPE((.|\n|\r)*?)\">", "");

Picked from here: http://www.gregbugaj.com/?p=270

like image 32
Murtaza Ashraf Avatar answered Sep 24 '22 02:09

Murtaza Ashraf