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 7963395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:35:56+00:00 2026-06-04T05:35:56+00:00

I have just discovered boost::property_tree, which seems the perfect answer to my problem. I

  • 0

I have just discovered boost::property_tree, which seems the perfect answer to my problem. I wrote a small test program to extract specific data from an xml file. I have used the example provided in the documentation as a guide.
The xml file: test.xml:

<section>
    <GROUP>
        <g_name>ABC</g_name>
        <fields>
            <row>
                <name>A</name>
                <datatype>string</datatype>
                <field_size>6</field_size>
                <value>ABC</value>
            </row>
            <row>
                <name>B</name>
                <datatype>integer</datatype>
                <field_size>5</field_size>
                <value>00107</value>
            </row>
            <row>
                <name>C</name>
                <datatype>string</datatype>
                <field_size>20</field_size>
                <value>LOTS OF LETTERS     </value>
            </row>
        </fields>
    </GROUP>
    <GROUP>
        <g_name>CDE</g_name>
        <fields>
            <row>
                <name>A</name>
                <datatype>string</datatype>
                <field_size>6</field_size>
                <value>CDE</value>
            </row>
            <row>
                <name>B</name>
                <datatype>integer</datatype>
                <field_size>5</field_size>
                <value>00100</value>
            </row>
            <row>
                <name>F</name>
                <datatype>integer</datatype>
                <field_size>4</field_size>
                <value>1970</value>
            </row>
        </fields>
    </GROUP>
</section>

The code:

        using boost::property_tree::ptree;
        struct t_collection
        {
            ptree pt;
            void load(const std::string &filename);
            void print();
        };
        void t_collection::load(const std::string &filename)
        {
            read_xml(filename, pt);
        }
        void t_collection::print()
        {
                BOOST_FOREACH(ptree::value_type &v, pt.get_child("section.GROUP"))
BOOST_FOREACH(ptree::value_type &v, pt.get_child("section.GROUP"))
{
    printf("X: %s->", v.second.data().c_str());
    //prints X: ABC ->
    BOOST_FOREACH(ptree::value_type &w, pt.get_child("section.GROUP.fields.row"))
        printf("%s\n", w.second.data().c_str());
    //prints A, string, 6, ABC - that is good for first iteration but there should be 3 iterations here
}
//then prints X: and just "" and repeats the set from the first one 
        }
        int main()
        {
            try
            {
                t_collection t1;
                t1.load("test.xml");
                t1.print();
            }
            catch (std::exception &e)
            {
                std::cout << "Error: " << e.what() << "\n";
            }
            return 0;
        } 

Note: I am trying to extract the values (ABC and the inner values, like A – string – 6 – ABC, for each GROUP – and each set of “row”, which I will process and then output in a different format). Please see comment in code for something I tried.
So far the best result was with: (contents inside print():

BOOST_FOREACH(ptree::value_type &z, pt.get_child("section"))
    //BOOST_FOREACH(ptree::value_type &v, pt.get_child("section.GROUP"))
        {
            printf("X: %s->", pt.get<std::string>("section.GROUP.g_mame", "default").c_str());
            //prints X: ABC ->
            BOOST_FOREACH(ptree::value_type &w, pt.get_child("section.GROUP.fields.row"))
            {
                printf("%s\n", pt.get<std::string>("section.GROUP.fields.row.name", "name").c_str());
                printf("%s\n", pt.get<std::string>("section.GROUP.fields.row.datatype", "type").c_str());
                printf("%s\n", pt.get<std::string>("section.GROUP.fields.row.field_size", "size").c_str());
                printf("%s\n", pt.get<std::string>("section.GROUP.fields.row.value", "value").c_str());
            }
        }
        //prints x: default->A, string, 6, ABC (3 times) then repeat identically

I can’t get the data from more than one record ! Please help, give me a suggestion – what am I doing wrong ?
Thank you.

  • 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-06-04T05:35:57+00:00Added an answer on June 4, 2026 at 5:35 am

    You are missing a level in your iteration. You need to iterate over the elements that have multiple children with the same name.

    std::pair<ptree::const_assoc_iterator, ptree::const_assoc_iterator>
        r(pt.get_child("section").equal_range("GROUP"));
    
    for (ptree::const_assoc_iterator i(r.first); i != r.second; ++i) {
        // Do something with each group.
    }
    

    Repeat as appropriate as you descend the tree.

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

Sidebar

Related Questions

I have just discovered a package for R to retrieve abstract from pubmed, which
I have just discovered that I don't think my nhibernate setup seems to be
I have just discovered a variable name that is misspelled. If it were hidden
I just discovered the what c# knowledge should I have? question and wondered about
I have a website, and I just discovered that somehow someone injected JavaScript on
I have just created a report in Report Manager using a Stored Procedure which
I have just begun working on a project which uses Mercurial as a version
I have just discovered the command :sort n in vim (how did I not
I have just discovered PDO and I'm very excited about it, but I have
I have just discovered live events in jQuery. I am trying it out. I

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.