Read tags from a xml string
-
Hello, all right?
I have the string below on a QString.
qDebug() << data; "<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:listarAlgo xmlns:ns2="http://soap.myws.com.br/"><return><codigo>1111</codigo><dataNascimento>1999-01-01</dataNascimento><email>my@email.com</email><nome>MyName</nome><sexo>F</sexo><grauInstrucao/><identificador>349</identificador><usuario><codigo>111</codigo><email>my@email.com</email><login>MyLogin</login><nome>MyName</nome><senha>Seinha</senha></usuario></return></ns2:listarAlgo></soap:Body></soap:Envelope>"
And I need get the text value of tags into "<return></return>"
QString data = ... string above QXmlStreamReader xml(data); while(!xml.atEnd()){ qDebug() << xml.name(); qDebug() << xml.readElementText(); xml.readNext(); }
But this code not iterate over all elements.
It prints only:"" "" "Envelope"
then exit of while loop.
also tried otherwise with "xml.readNextStartElement();" outside while loop, and he return "Body" and exit while loop...
Is there any way I can iterate over all the elements?My best regards!
-
-
SGaist, with that example I checked that an error stops the while loop:
QXmlStreamReader::UnexpectedElementError
Current code:
while(!xml.atEnd()){ qDebug() << xml.name(); qDebug() << xml.readElementText(); xml.readNext(); if (xml.hasError()) { qDebug() << xml.error(); //Return 1 = QXmlStreamReader::UnexpectedElementError } }
The xml to be parsed:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:listarAlgo xmlns:ns2="http://soap.myws.com.br/"> <return> <codigo>1111</codigo> <dataNascimento>1999-01-01</dataNascimento> <email>my@email.com</email> <nome>MyName</nome> <sexo>F</sexo> <grauInstrucao/> <identificador>349</identificador> <usuario><codigo>111</codigo> <email>my@email.com</email> <login>MyLogin</login> <nome>MyName</nome> <senha>Seinha</senha> </usuario> </return> </ns2:listarAlgo> </soap:Body> </soap:Envelope>
Output console: "" "" "Envelope" 1
-
- Find the required tag (in your case <return> tag).
- Process the data.
while(xml.tokenType() != QXmlStreamReader::EndDocument) { if(xml.tokenType() == QXmlStreamReader::StartElement && (xml.name().compare("return", Qt::CaseInsensitive)==0)) { // ------------- todo --------------------- // Process the data } xml.readNext(); }
-
You code works so fine!
How can I get all tags inside <return> and get childs of <usuario></usuario> ?<return> <<<<<<<<<<<<<< <codigo>1111</codigo> <dataNascimento>1999-01-01</dataNascimento> <email>my@email.com</email> <nome>MyName</nome> <sexo>F</sexo> <grauInstrucao/> <identificador>349</identificador> <usuario> < <<<<<<<<<<<<<<<< <codigo>111</codigo> <email>my@email.com</email> <login>MyLogin</login> <nome>MyName</nome> <senha>Seinha</senha> </usuario> </return>
My best regards!
-
You have to get down five level deep:
- Envelope
- Body
- listarAlgo
- return
- usuario
The easiest way is to make a function that parses each element and call it once you found it. Again the Bookmark example shows that nicely.