[solved] QDomNode get formated text
- 
Hi all, 
 I'm currently parsing a xml file. I get the content of the QDomeNodes with node.toElement().text(). So far so good.
 But I have several nodes which have some formating tags. Those are stripped by the toElement().text() function.
 Is there a way to get the content of a node with those informations?
 Best,
 Patrik
- 
Hi all, 
 I'm currently parsing a xml file. I get the content of the QDomeNodes with node.toElement().text(). So far so good.
 But I have several nodes which have some formating tags. Those are stripped by the toElement().text() function.
 Is there a way to get the content of a node with those informations?
 Best,
 Patrik@patrikd 
 See the example at https://doc.qt.io/qt-5/qdomelement.html#text for how only the text part is returned.It only evaluates QDomTextandQDomCDATASectionobjectsYou must iterate/recurse through all QDomNodechildren (QDomNode::childNodes()) if you want to visit all non-pure-text nodes.EDIT So, to be clear. Totally untested by me, but if you have, say, <b>Bold</b>thenQDomElement::text()will only returnBold, which is the only text within the node/element. But if you visit the descendants individually you should find you have aQDomElementwithnodeName() == "b"and that has a childQDomTextwithnodeValue()/text() == "Bold". You can use thatnodeName() == "b"if you want to recognise the bold tag or reconstruct the<b>...</b>for yourself.
- 
Hi JonB, 
 thx for the tip. Works fine for me :)
 And for those who run into the same:QString concatedText = ""; if (node.hasChildNodes()){ concatedText = ""; QDomNode chld = node.firstChild(); QString tagText = "<%1>%2</%3>"; while (!chld.isNull()) { QString tag = chld.nodeName(); if (tag == "b" || tag == "i" || tag == "u"){ QString val = chld.toElement().text(); QString replacement = tagText.arg(tag).arg(val).arg(tag); concatedText.append(replacement); }else if (tag == "emph"){ QString attr = chld.toElement().attribute("type").trimmed(); if (attr == "under"){ QString val = chld.toElement().text(); QString replacement = tagText.arg("u").arg(val).arg("u"); concatedText.append(replacement); } } else if (tag == "br"){ concatedText.append("<br/>"); } else if (chld.isText()){ QString txt = chld.toText().data(); concatedText.append(txt); } chld = chld.nextSibling(); } }Best, 
 Patrik
- 
Hi JonB, 
 thx for the tip. Works fine for me :)
 And for those who run into the same:QString concatedText = ""; if (node.hasChildNodes()){ concatedText = ""; QDomNode chld = node.firstChild(); QString tagText = "<%1>%2</%3>"; while (!chld.isNull()) { QString tag = chld.nodeName(); if (tag == "b" || tag == "i" || tag == "u"){ QString val = chld.toElement().text(); QString replacement = tagText.arg(tag).arg(val).arg(tag); concatedText.append(replacement); }else if (tag == "emph"){ QString attr = chld.toElement().attribute("type").trimmed(); if (attr == "under"){ QString val = chld.toElement().text(); QString replacement = tagText.arg("u").arg(val).arg("u"); concatedText.append(replacement); } } else if (tag == "br"){ concatedText.append("<br/>"); } else if (chld.isText()){ QString txt = chld.toText().data(); concatedText.append(txt); } chld = chld.nextSibling(); } }Best, 
 Patrik@patrikd 
 Looking at your code, I wonder if you are trying to reinvent the wheel! :) You seem to be just going through aQDomNode(or whatever) and reconstructing the HTML which did/could have generated it. Are you aware that, say, QString QDomDocument::toString() can be used the other way round, to turn a dom document back to corresponding HTML, instead of you trying to do the (incomplete) work?Converts the parsed document back to its textual representation. 
- 
If you just need something like outerHTMLin javascript, you can use QDomNode::save like:QString output; QTextStream stream(&output); node.save(stream, 0);
 

