HTML tags are getting converted

笑着哭i 提交于 2019-12-23 16:21:05

问题


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

 &lt;STRONG&gt;this&lt;/STRONG&gt;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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!