hhurz / tableExport.jquery.plugin

jQuery plugin to export a html table to JSON, XML, CSV, TSV, TXT, SQL, Word, Excel, PNG and PDF
MIT License
984 stars 714 forks source link

Invalid XML content #345

Closed qgribble closed 2 years ago

qgribble commented 2 years ago

I have an issue with unsafe characters in cell content. I encode content using bootstrap-table's data-escape="true" feature. The problem is when I export to XML, the content is not escaped and I get an invalid xml document.

Anyone else had this problem? Is there a workaround?

hhurz commented 2 years ago

This plugin is not specific to Bootstrap, so it doesn't know about Bootstraps data escape option. What you can do is implement the callback function onCellHtmlData and change (escape) the cell content yourself.

hhurz commented 2 years ago

Suppose you want to export this table: grafik

If you implement the callback function onCellData:

{type: 'xml', onCellData: DoOnXmlCellData}
function DoOnXmlCellData(cell, row, col, data) {
  if (data == null || data.length === 0) {
    return data;
  }
  return data
          .replace(/&/g, "&")
          .replace(/</g, "&lt;")
          .replace(/>/g, "&gt;")
          .replace(/"/g, "&quot;")
          .replace(/'/g, "&#039;");
}

the result would be:

<?xml version="1.0" encoding="utf-8"?>
<tabledata>
  <fields>
    <field>column 1</field>
    <field>column 2</field>
  </fields>
  <data>
    <row id="1">
      <column-1>A</column-1>
      <column-2>Line 1
Line 2
Line 3
Line 4</column-2>
    </row>
    <row id="2">
      <column-1>B</column-1>
      <column-2>Line 1 TeableExport&#039;s Line 2 Line 3 contains &#039; and &quot;</column-2>
    </row>
    <row id="3">
      <column-1>C</column-1>
      <column-2>Text with &quot;unsafe&quot; characters: &lt;&amp;&gt;</column-2>
    </row>
    <row id="4">
      <column-1>D</column-1>
      <column-2>Text content</column-2>
    </row>
  </data>
</tabledata>

If you implement the callback function onCellHtmlData:

{type: 'xml', onCellData: onCellHtmlData}

the result would be:

<?xml version="1.0" encoding="utf-8"?>
<tabledata>
  <fields>
    <field>column 1</field>
    <field>column 2</field>
  </fields>
  <data>
    <row id="1">
      <column-1>A</column-1>
      <column-2>Line 1<br> Line 2<br ng-if="!$last" class="ng-scope"> Line 3<br> Line 4</column-2>
          </row>
          <row id="2">
            <column-1>B</column-1>
            <column-2>Line 1 TeableExport's Line 2 Line 3 contains ' and "</column-2>
          </row>
          <row id="3">
            <column-1>C</column-1>
            <column-2>
              <span>Text with "unsafe" characters: &lt;&amp;&gt;</span>
            </column-2>
          </row>
          <row id="4">
            <column-1>D</column-1>
            <column-2>
              <span>Text content</span>
            </column-2>
          </row>
        </data>
      </tabledata>