有没有一种简单的方法从Qt中的QString去除HTML? [英] Is there an easy way to strip HTML from a QString in Qt?
问题描述
我有一个带有一些HTML的QString ...有没有一种简单的方法从它中剥离HTML?
I have a QString with some HTML in it... is there an easy way to strip the HTML from it? I basically want just the actual text content.
<i>Test:</i><img src="blah.png" /><br> A test case
会变成:
Would become:
Test: A test case
我很想知道if Qt有一个字符串函数或实用程序。
I'm curious to know if Qt has a string function or utility for this.
推荐答案
您可以尝试使用QXmlStreamReader类遍历字符串并提取所有文本(如果你的HTML字符串是保证格式良好的XML)。
You may try to iterate through the string using QXmlStreamReader class and extract all text (if you HTML string is guarantied to be well formed XML).
类似于这样:
Something like this:
QXmlStreamReader xml(htmlString);
QString textString;
while (!xml.atEnd()) {
if ( xml.readNext() == QXmlStreamReader::Characters ) {
textString += xml.text();
}
}
但我不确定它的100% QXmlStreamReader API的使用,因为我已经用了很长时间,可能会忘记一些东西。
but I'm unsure that its 100% valid ussage of QXmlStreamReader API since I've used it quite longe time ago and may forget something.
这篇关于有没有一种简单的方法从Qt中的QString去除HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!