1

Good day to everybody!

I have this sort of HTML file:

  <tr>
    <td>
      <p>First name: </p>
    </td>
    <td>
      <p> %first_name% </p>
    </td>
  </tr>
  <tr>
    <td>
      <p>Last name: </p>
    </td>
    <td>
      <p"> %last_name% </p>
    </td>
  </tr>

I'm looking for a way of replacing special markers of type(%smth%) by concrete data. Project's being developed under Qt, so I wonder if some Qt's methods can do it. Thanks!

Sas
  • 523
  • 3
  • 21

2 Answers2

1

The simplest solution might be using QString & QString::replace ( const QString & before, const QString & after, Qt::CaseSensitivity cs = Qt::CaseSensitive ) which replaces every occurrence of the string before with the string after and returns a reference to this string.

Place the contents of your html file into a QString then call QString::replace() to replace the special markers by concrete data. For example:

QString firstName("John");
html.replace("%first_name%", firstName);
Bill
  • 11,595
  • 6
  • 44
  • 52
0

As far as you can not use regexps, I recommend using XSLT which supported by xmlpatterns library.

EDIT

As someone thinks he still can parse html with regexp in this case, I will give some examples, that will show regexps fail:

  1. You have marker in attribute (and you don't want it to be replaced)

    <p class="%first name">
    
  2. Someone would deside to inject:

    map: %firstname -> <srcipt language ="javascript">....</script>
    

    After XSLT substitution will be escaped automatically.

Community
  • 1
  • 1
Lol4t0
  • 12,444
  • 4
  • 29
  • 65