问题
I have the following code snippet to have output from XML data which is stored in the database table
ServletOutputStream os = response.getOutputStream();
String contentDisposition = "attachment;filename=Test.HTML";
response.setHeader("Content-Disposition",contentDisposition);
response.setContentType("text/html");
XMLNode xmlNode = (XMLNode)am.invokeMethod("getDataXML");
ByteArrayOutputStream outputStream =
new ByteArrayOutputStream();
xmlNode.print(outputStream);
ByteArrayInputStream inputStream =
new ByteArrayInputStream(outputStream.toByteArray());
ByteArrayOutputStream pdfFile = new ByteArrayOutputStream();
TemplateHelper.processTemplate(((OADBTransactionImpl)pageContext.getApplicationModule(webBean).getOADBTransaction()).getAppsContext(),
"INV",
"MyTemplate",
((OADBTransactionImpl)pageContext.getApplicationModule(webBean).getOADBTransaction()).getUserLocale().getLanguage(),
((OADBTransactionImpl)pageContext.getApplicationModule(webBean).getOADBTransaction()).getUserLocale().getCountry(),
inputStream,
TemplateHelper.OUTPUT_TYPE_HTML,
null, pdfFile);
byte[] b = pdfFile.toByteArray();
response.setContentLength(b.length);
os.write(b, 0, b.length);
os.flush();
os.close();
pdfFile.flush();
pdfFile.close();
public XMLNode getDataXML() {
OAViewObject vo = (OAViewObject)findViewObject("DataVO");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
XMLNode xmlNode =
(XMLNode)vo.writeXML(4, XMLInterface.XML_OPT_ALL_ROWS);
return xmlNode;
}
I have HTML tags which is stored in the table as
<STRONG>this</STRONG> is only a test.
However the above is getting converted to
<STRONG>this</STRONG>is only a test.
How can I preserve the original HTML tags when I execute the code or how do I convert it back to the original without using any third party libraries as we have a restriction of using third party libraries in the server.
回答1:
take a look at this for more information
The HTML character encoder converts all applicable characters to their corresponding HTML entities. Certain characters have special significance in HTML and should be converted to their correct HTML entities to preserve their meanings. For example, it is not possible to use the < character as it is used in the HTML syntax to create and close tags. It must be converted to its corresponding < HTML entity to be displayed in the content of an HTML page. HTML entity names are case sensitive.
and then this may help you :
use the Apache Commons StringEscapeUtils.unescapeHtml4()
for this:
Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. Supports HTML 4.0 entities.
Edit
it seems the java itself has this method
URLDecoder.decode(String stringToDecode)
and this
URLDecoder.decode(String stringToDecode, String charset);
hope this works for you
来源:https://stackoverflow.com/questions/40815286/html-tags-are-getting-converted