简体   繁体   English

BIRT 在脚本中将 HTML 文本转换为纯文本

[英]BIRT converting HTML text to plain text in scripts

I am attempting to convert HTML entities/tags from a database column into plain text to be displayed in a report.我试图将 HTML 实体/标签从数据库列转换为纯文本以显示在报告中。 The report is dynamic and as such is created using scripting functions in BIRT.该报告是动态的,因此是使用 BIRT 中的脚本功能创建的。

Since there is no layout displayed I am unable to change the type of the label/text to HTML (as far as I am aware).由于没有显示布局,我无法将标签/文本的类型更改为 HTML(据我所知)。 I have tried a few things but nothing has worked properly.我尝试了几件事,但没有任何工作正常。 Here is what I am attempting to do - any advice or direction would be very appreciated.这是我正在尝试做的 - 任何建议或方向将不胜感激。 Please note I cannot include any 3rd party libraries, but can use anything that comes as fairly standard.请注意,我不能包含任何 3rd 方库,但可以使用任何相当标准的库。

function decodeHTML(html) {
    var dbf = DocumentBuilderFactory.newInstance();
    var builder = dbf.newDocumentBuilder();
    var doc = builder.newDocument();

    var txt = doc.createElement("textarea");
    txt.innerHTML = html; //does not work (think this is not avaiable in Java)
    return txt.value;
}

*Its worth noting - any other ideas not involving Java or DOM are also acceptable. *值得注意 - 任何其他不涉及 Java 或 DOM 的想法也是可以接受的。 This was just one of my attempts at solving this issue.这只是我解决这个问题的尝试之一。

You can use this function to escape HTML:您可以使用此函数来转义 HTML:

function escapeHTML(str){
   return str.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
    return '&#'+ i.charCodeAt(0)+ ';';
  });
}

 function escapeHTML(str){ return str.replace(/[\ -\香<>\\&]/gim, function(i) { return '&#'+ i.charCodeAt(0)+ ';'; }); } var escaped = escapeHTML("<h1>Header</h1>"); console.log(escaped); document.body.innerHTML = escaped;
 <body></body>

To decode HTML entities, you can use DOMParser .要解码 HTML 实体,您可以使用DOMParser

function decodeHTML(str){
  var doc = new DOMParser().parseFromString(str, "text/html");
  return doc.documentElement.textContent;
}

 function decodeHTML(str){ var doc = new DOMParser().parseFromString(str, "text/html"); return doc.documentElement.textContent; } var decoded = decodeHTML("&#60;h1&#62;Header&#60;/h1&#62;"); console.log(decoded); document.body.innerHTML = decoded;
 <body></body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM