QT - Parse XML with html reference notation of umlauts

246 Views Asked by At

I hope anybody can help me.

My problem is that I get XML with html entities for special charakters like:

<person>
  <firstname>Max</firstname>
  <lastname>M&uuml;ller</lastname>
</person>
<person>
  <firstname>Bernd</firstname>
  <lastname>Sch&auml;fer</lastname>
</person>

I find no way in QT to decode the "&uuml" to a normal "ü". In the QT-DomTree this entity will stand in a QDomEntityRefrence object wich has no getter or other output or parse functionality. I use standard way to parse the XML tree

QDomDocument doc;
if (!doc.setContent(response, &errors))
return false;

QDomElement const & root = doc.firstChildElement("person");
for (QDomElement xmlPerson= root.firstChildElement("person"); !xmlPerson.isNull(); xmlPerson = xmlPerson.nextSiblingElement("person"))
{
    QDomNodeList personCont = xmlPerson.childNodes();
    PersonObj person;

    for(int i = 0; i < personCont.count(); i++)
    {
        QDomNode itemNode = personCont.at(i);
        if(itemNode.isElement()){
            QDomElement item = itemNode.toElement();
            if(item.tagName() == "firstname")
            {
                person.setFirstname(item.firstChild().text());
            }
            else if(item.tagName() == "lastname")
            {
                addressBook.setLastname(item.firstChild().text());
            }
                ...

Result:

Max Mller

Bernd Schfer

Thanks for your greate awnsers

1

There are 1 best solutions below

0
On

Use QTextDocument()

QTextDocument doc;
doc.setHtml("Sch&auml;fer");
qDebug()<<doc.toPlainText();

In your example

QTextDocument doc;

switch(item.tagName())
        {
        case "firstname":
            doc.setHtml(item.firstChild().text());
            person.setFirstname(doc.toPlainText());
            break;
        case "lastname":
            doc.setHtml(item.firstChild().text());
            addressBook.setLastname(doc.toPlainText());
            break;
            ...