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!


  • Lifetime Qt Champion

    Hi,

    You have the XBEL example that you can take inspiration from.

    Hope it helps



  • 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
    


    1. Find the required tag (in your case <return> tag).
    2. 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();
        }
    
    


  • @Bagavathi

    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!



  • @Helson

    Please refer the below Article, this may help you lot.

    Qt5 Tutorial QtXML DOM Reading - 2015



  • Thank you, my first question is solved.

    My second is: how can I get all child of <return><return>?


  • Lifetime Qt Champion

    You have to get down five level deep:

    1. Envelope
    2. Body
    3. listarAlgo
    4. return
    5. 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.


Log in to reply
 

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