Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7686287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:26:42+00:00 2026-05-31T19:26:42+00:00

how can i count the parents (or entries) in an xml file? The XML

  • 0

how can i count the parents (or entries) in an xml file?

The XML below should show that I don’t know the entries in there so I need to count the tags to use them in a loop and save them in a QVector. From there on I could search the xml for a (now known) tag and get the childs and attributes (but thats another thing).
First step is to count the parents.

If anyone has a better solution then please let me know!

<?xml version="1.0" encoding="UTF-8" ?>
<Content>
    <person1>
        <firstname>John</firstname>
        <surname>Doe</surname>
        <email>john.doe@example.com</email>
        <website>http://en.wikipedia.org/wiki/John_Doe</website>
    </person1>
    <person2>
        <various>Some Text here.</various
        <website>http://www.google.com</website>
    </person2>
    <person3>
        <firstname>Jane</firstname>
        <surname>Doe</surname>
        <email>jane.doe@example.com</email>
        <website>http://en.wikipedia.org/wiki/John_Doe</website>
    </person3>
    <person4>
        <firstname>Matti</firstname>
        <surname>Meikäläinen</surname>
        <email>matti.meikalainen@example.com</email>
        <website>http://fi.wikipedia.org/wiki/Matti_Meikäläinen</website>
    </person4>
    <person5 Attribute="Test">
    </person5>
</Content>

I’ve tried to open the file and read it line per line while search for an RegExp (with less success) like one would do it in bash. But there must be a better solution with the XML functions from Qt4. Any suggestios?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:26:44+00:00Added an answer on May 31, 2026 at 7:26 pm

    I’ve found a solution. It’s not very nice but it works:

        QFile* file = new QFile("SomeFile.xml");
        QXmlStreamReader xml(file);
    
        QString ElementName = "Content";
        QString ElementName_2 = "Content";
        int i = 0;
        int k = 0;
    
        while( !xml.atEnd() && !xml.hasError() )
        {
            QXmlStreamReader::TokenType token = xml.readNext();
    
            if( token == QXmlStreamReader::StartElement )
            {
                if( xml.name().toString() != ElementName )
                {
                    ElementName = xml.name().toAscii();
                }
                if( xml.name().toString() == ElementName_2 )
                {
                    ElementName = xml.name().toAscii();
                }
            }
            if( token == QXmlStreamReader::EndElement )
            {
                if( xml.name().toString() == ElementName )
                {
                    i++;
                }
                if( xml.name().toString() != ElementName_2 )
                {
                    k++;
                }
            }
        }
    
        if(xml.hasError())
        {
            SCC_MsgBox(QString("%1 parsing error").arg(XMLname), "", xml.errorString(), "", 2, 3); // my own MsgBox implementation ...
            return 0;
        }
        else
        {
            i = k - i;
            return i;
        }
    
        xml.clear();
    

    This one counts all Elements (k) and all Childs (i) and substracts Childs from Elements. So i get the amount of entries (persons in this case). If someone has a better solution then please post it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I count on string arrays within the strings.xml resource file to be parsed/deserialized
I can count the rows in the dataset that the table consumes, but this
I would like to know how I can count the number of unique values
How can I count all connections (clients) that are connected to the specified port
Is there a way I can count the size of an associated collection without
I have a table and can count the number of rows using var count
I have a SQL which can count the condition(Bad,Poor,Worse) in a day and group
I am returning form elements into a form from ajax. I can count the
How can I count the number of occurrences in a C string of /
how can I count single words appearing in a field of a resultset? example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.