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. [SOLVED] Could not extract specific Item from XML file using QXmlStreamReader !!
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Could not extract specific Item from XML file using QXmlStreamReader !!

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.4k 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.
  • R Offline
    R Offline
    rafaelSorel
    wrote on last edited by
    #1

    Hello Guys,

    I'm facing a problem extracting data from xml file, here is the example:
    @
    <item>
    <title>tilte 1</title>
    <link>http://example1.com</link>
    <description>description 1</description>
    <pubDate>20 Aug 2014 10:00:00 +0400</pubDate>
    </item>

    <item>
    <title>tilte 2</title>
    <link>http://example_2.com</link>
    <description>description 2</description>
    <pubDate>21 Aug 2014 10:30:00 +0400</pubDate>
    </item>

    <item>
    <title>tilte 3</title>
    <link>http://example_3.com</link>
    <description>description 3</description>
    <pubDate>20 Aug 2014 11:30:00 +0400</pubDate>
    </item>
    @

    I want to extract an <item> that contains a <pubDate> of 20 Aug 2014 using this part of code:

    @
    while(!xml.atEnd() && !xml.hasError())
    {
    /* Read next element./
    QXmlStreamReader::TokenType token = xml.readNext();
    /
    If token is just StartDocument, we'll go to next./
    if(token == QXmlStreamReader::StartDocument)
    {
    continue;
    }
    /
    If token is StartElement, we'll see if we can read it./
    if(token == QXmlStreamReader::StartElement)
    {
    /
    If it's named tv, we'll go to the next./
    if(xml.name() != "item")
    {
    continue;
    }
    /
    If it's named item, we'll dig the information from there.*/
    if(xml.name() == "item")
    {
    //Check Date
    if(this->checkItem(xml))
    {
    items.append(this->parseItem(xml));
    }
    }
    }
    }

    bool checkItem(QXmlStreamReader& xml)
    {
    if(xml.tokenType() != QXmlStreamReader::StartElement && xml.name() == "item")
    {return false;
    }

    /* Next element... */
    xml.readNext();
    /*
     * We're going to loop over the things because the order might change.
     * We'll continue the loop until we hit an EndElement named item.
     */
    while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "item"))
    {
        if(xml.tokenType() == QXmlStreamReader::StartElement)
        {
            if(xml.name() == "pubDate")
            {
                //Check Date
                xml.readNext();
                if(xml.tokenType() != QXmlStreamReader::Characters)
                {
                    return false;
                }
                if(!xml.text().toString().startsWith("20 Aug 2014"))
                {
                    qDebug() << "Check Date NOK";
                    return false;
                }
                qDebug() << "output Element" << OutputElement;
                qDebug() << "Check Date OK";
            }
        }
        xml.readNext();
    }
    return true;
    

    }
    @

    The thing is the readNext() method update the reader cursor so that when returning from checkItem function I can not parse and store the checked <item> using parseItem() .

    Even when tried to use local QXmlStreamReader when using the checkItem() method , I discover that this Class could not be copied Q_DISABLE_COPY(QXmlStreamReader) !!! I just wanted to copy items that contains specific <pubDate>.

    I have also triyed to use readElementText but it appears that this method update the xml reader cursor as well.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      You can check with example given "here":http://developer.nokia.com/community/wiki/QXmlStreamReader_to_parse_XML_in_Qt

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • JohanSoloJ Offline
        JohanSoloJ Offline
        JohanSolo
        wrote on last edited by
        #3

        [quote author="rafaelSorel" date="1408572972"]The thing is the readNext() method update the reader cursor so that when returning from checkItem function I can not parse and store the checked <item> using parseItem() .[/quote]

        Then why don't you extract the date directly in the checkItem function? The basic idea behind QXmlStreamReader is that once you've accessed the data, you cannot read it again. If you want to do this, you will have to use the QXmlDomDocument et al API.

        `They did not know it was impossible, so they did it.'
        -- Mark Twain

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rafaelSorel
          wrote on last edited by
          #4

          :Johan Solo: Hello , I want to check the date and if it matches I have to extract the whole <item> but when I read the date it is late to extract the item because the cursor has been updated.

          :Dheerendra: I already checked the link that you have provide but it is the same case. The guy extract all the content of the xml and he doesn't make a check on any token before extract.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rafaelSorel
            wrote on last edited by
            #5

            Well I have succeded to check and extract what I wanted by using QDomDocument Module:

            @
            for(int i = 0; i < nodeList.count(); i++)
            {
            // get the current one as QDomElement
            QDomElement el = nodeList.at(i).toElement();

                QDomNode pDateEntry = el.lastChild();
                if(pDateEntry.toElement().tagName() == "pubDate")
                {
                    //Check date Node before proceed
                    if(!pDateEntry.toElement().text().contains("Wed, 20 Aug 2014"))
                    {
                        qDebug() << "Date changed Finish extracting" << pDateEntry.toElement().text();
                        return;
                    }
                }
            
                if(!this->checkItem(el))
                {
                    qDebug() << "Continue...";
                    continue;
                }
                qDebug() << "Store current Item";
                items.append(this->parseItemAndStore(el));
            }
            

            @

            Thx guys for help :)

            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