Parse response of QNetworkAccessManager using QtWebEngine



  • Hi guys, I'm making a request using QNetworkAccessManager to download a page and I get the html of that page with all the tags. That is how the page looks like:

    <!DOCTYPE html>
    <html>
    <head>
    	<title>Webservice</title>
    </head>
    <body>
    <p>
    	USER = JOHN DOE
    	AGE = 21
    	MOTHER = JANE DOE
    	BROTHERS = 2
    </p>
    </body>
    </html>
    

    I want to load this HTML in a QWebEngine (that is new to me) and get the content that is inside the <p>.
    The tricky part for me is, I want to parse that content like key = value and put in a QMap<QString, QString>;
    For example, in this case it would be: QMap<QString, QString> info = { { "USER": "JOHN DOE" } };
    How can I do that? I already have the content using the QNetworkAccessManager, now the only thing left is get the content of the tag and parse it into a QMap.


  • Qt Champions 2016

    @Volebab

    now the only thing left is get the content of the tag and parse it into a QMap.

    If you have the HTML, you could try to use the QDomDocument class and load it there. Then you can traverse the document by tags like in the example from the documentation.



  • @kshegunov Thank you, but what about the tricky part? I mean, parse the key values to a qmap?


  • Qt Champions 2016

    @Volebab
    Well, depends on the format, but if it's like shown (i. e. being a simple one) you can split the <p> tag's contents first by newlines, so you get each pair NAME = VALUE. And then for each such pair you can split it again by the equality sing, so you retrieve the two parts - the name and the value.



  • @kshegunov You are right, how couldn't I thought about that before? I thought about using regex like (.*) = (.*) but I think that it will be more expansive than using split.


  • Qt Champions 2016

    @Volebab said:

    I thought about using regex like (.) = (.)

    Regex would probably work as well. So it's a matter of choice I suppose.



  • @kshegunov Which one is faster? Do you have an idea?


  • Qt Champions 2016

    @Volebab said:

    Which one is faster? Do you have an idea?

    No, not really. Usually regex expressions are somewhat slower, but I can't be sure for this case. If it's really important you should write a simple program to benchmark it.



  • @kshegunov I will, but anyway, I did it thanks to you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.