Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Read tags from a xml string
Forum Updated to NodeBB v4.3 + New Features

Read tags from a xml string

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Helson
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Helson
        wrote on last edited by Helson
        #3

        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 Reply Last reply
        0
        • B Offline
          B Offline
          Bagavathi
          wrote on last edited by
          #4
          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();
              }
          
          
          H 1 Reply Last reply
          2
          • B Bagavathi
            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();
                }
            
            
            H Offline
            H Offline
            Helson
            wrote on last edited by Helson
            #5

            @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!

            B 1 Reply Last reply
            0
            • H Helson

              @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!

              B Offline
              B Offline
              Bagavathi
              wrote on last edited by
              #6

              @Helson

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

              Qt5 Tutorial QtXML DOM Reading - 2015

              1 Reply Last reply
              1
              • H Offline
                H Offline
                Helson
                wrote on last edited by Helson
                #7

                Thank you, my first question is solved.

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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved