2016-04-01 15 views
0

QString'de tutulan HTML verilerini işlemeye çalışıyorum. Veriler, HTML etiketlerini, örn. "<" vb. Bunları uygun sembollere dönüştürmek istiyorum.QString.replace çalışmıyor

Birçok yaklaşımı deniyorum, ancak hiçbiri işe yaramıyor, bu da gerçekten basit bir şeyi kaçırdığımı gösteriyor.

QString theData = "&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt; 
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt; 
p, li { white-space: pre-wrap; } 
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Arial'; font-size:20pt; font-weight:400; font-style:normal;&quot;&gt; 
&lt;table border=&quot;0&quot; style=&quot;-qt-table-type: root; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;&quot;&gt; 
&lt;tr&gt; 
&lt;td style=&quot;border: none;&quot;&gt; 
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:14pt; color:#4cb8ff;&quot;&gt;This is text on the second page. This page contains a embedded image,&lt;/span&gt;&lt;/p&gt; 
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:14pt; color:#4cb8ff;&quot;&gt;and audio.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;"; 

QString t2 = theData.replace("&amp;", "&").replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "'"); 

t2 değeri ancak cümledeki sonra theData aynıdır: Burada

(önceki yorum tarafından bildirilen yazım hatalarını düzeltmek için değiştirilen) kodudur.

+0

"t1..replace" <- Geçerli bir C++ sözdizimine benzemiyor. – MrEricSir

cevap

0

Kodunuzda t1 tanımı yok, sanırım theData (ve double dot yok) demek istediniz. QString :: replace işlevleri dizenin değerini değiştirir ve bunun bir referansını döndürür.

QString s = "abc"; 
s.replace("a", "z").replace("b", "z"); 
// s = "zzc"; 

// if you don't want to alter s 
QString s = "abc"; 
QString t = s; 
t.replace("a", "z").replace("b", "z"); 

Ama kaçmak için iyi bir yol/çıkış yapılmış html dizeleri vardır:

// html -> plain text 
QTextDocument doc; 
doc.setHtml(theData); 
QString t2 = doc.toPlainText(); 

// plain text -> html 
QString plainText = "#include <QtCore>" 
QString htmlText = plainText.toHtmlEscaped(); 
// htmlText == "#include &lt;QtCore&gt;" 

sadece html varlıkları dönüştürmek istiyorsanız, ben QString::toHtmlEscaped() tamamlayıcı aşağıdaki işlevi kullanın:

QString fromHtmlEscaped(QString html) { 
    html.replace("&quot;", "\"", Qt::CaseInsensitive); 
    html.replace("&gt;", ">", Qt::CaseInsensitive); 
    html.replace("&lt;", "<", Qt::CaseInsensitive); 
    html.replace("&amp;", "&", Qt::CaseInsensitive); 
    return html; 
} 

Her durumda, bu str == fromHtmlEscaped(str.toHtmlEscaped()) tutmalıdır.

+0

Değiştirmelerin sırası önemlidir, ve sonuncu değiştirilmelidir. İşlevi cevapta düzenledim, yorumu kaldırmaktan çekinmeyin. Genel olarak, bir soru/cevapta bir eksiklik olduğunu belirten yorumlar, sırasıyla soru/cevap düzeltilerek veya değiştirilerek ele alınmalıdır. –