简体   繁体   English

如何显示具有XML数据的字符串

[英]How to display string having XML data

My JSON data is: 我的JSON数据是:

[
    {
        "serviceName":"test",
        "requestXML":"<soapenvelope><uname>testuser</uname></soapenvelope>"
    },
    {
        "serviceName":"test2",
        "requestXML":"<soapenvelope><uname>testuser2</uname></soapenvelope>"
    } 
]

jQuery code to call to backend which returns this JSON data 用于调用后端的jQuery代码,它返回此JSON数据

var postRequest = $.post(url);

postRequest.done(function( data ) {

      $('#tableid').dataTable( {
         "processing": true,
          destroy: true,
          "aaData": data,
          "aoColumns": [
                { "data": "serviceName" },
                { "data": "requestXML" },
          ]
      });     
});

Now when it is shown on the screen as jQuery DataTable, I wanted the entire XML to be printed as it. 现在,当它在屏幕上显示为jQuery DataTable时,我希望将整个XML打印出来。 But it just prints testuser instead of the whole XML. 但它只打印testuser而不是整个XML。

Could anyone please help me with this as what is going wrong? 有人可以帮我解决这个问题吗?

I have verified my JSON data is going correctly. 我已经验证了我的JSON数据正确无误。

SOLUTION

You can use $('<div/>').text(data).html() trick to encode HTML entities. 您可以使用$('<div/>').text(data).html()技巧来编码HTML实体。

For example: 例如:

$('#tableid').dataTable({
    "processing": true,
    "destroy": true,
    "data": data,
    "columns": [
       { 
          "data": "serviceName" 
       },
       { 
          "data": "requestXML",
          "render": function(data, type, full, meta){
              return $('<div/>').text(data).html();
          }
       }
    ]
});

DEMO DEMO

See this jsFiddle for code and demonstration. 有关代码和演示,请参阅此jsFiddle

Your server should take care of the response what it is sending. 您的服务器应该处理它发送的响应。 instead of sending 而不是发送

[
{"serviceName":"test","requestXML":"<soapenvelope><uname>testuser</uname></soapenvelope>"},
 {"serviceName":"test2","requestXML":"<soapenvelope><uname>testuser2</uname></soapenvelope>"} 
]

it should send something like below 它应该发送如下的东西

[
    {"serviceName":"test","requestXML":"&lt;soapenvelope&gt;&lt;uname&gt;testuser&lt;/uname&gt;&lt;/soapenvelope&gt;"},
     {"serviceName":"test2","requestXML":"&lt;soapenvelope&gt;&lt;uname&gt;testuser2&lt;/uname&gt;&lt;/soapenvelope&gt;"} 
    ]

If that is not possible ( you don't control the server ) then the following link should help Escape markup in JSON-driven jQuery datatable? 如果那是不可能的(你不控制服务器),那么以下链接应该有助于在JSON驱动的jQuery数据表中使用Escape标记?

First of all you will need XML escaping function (taken from here ): 首先,您需要XML转义函数(取自此处 ):

var XML_CHAR_MAP = {
  '<': '&lt;',
  '>': '&gt;',
  '&': '&amp;',
  '"': '&quot;',
  "'": '&apos;'
}

function escapeXml (s) {
  return s.replace(/[<>&"']/g, function (ch) {
    return XML_CHAR_MAP[ch]
  })
}

Then you can render cell content using it: 然后,您可以使用它渲染单元格内容:

{
  data: "requestXML",
  render: function(data, method, object, pos) {
    return '<pre>' + escapeXml(data) + '</pre>'
  }
}

Now your XML should be visible. 现在您的XML应该是可见的。 Remove pre tags, if not required. 如果不需要,请删除pre标记。

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

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