English 中文(简体)
推进: 财产_ : 树木 - 分析与处理数据
原标题:boost::property_:tree - parsing and processing data

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>

代码:

        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
", 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() << "
";
            }
            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
", pt.get<std::string>("section.GROUP.fields.row.name", "name").c_str());
                printf("%s
", pt.get<std::string>("section.GROUP.fields.row.datatype", "type").c_str());
                printf("%s
", pt.get<std::string>("section.GROUP.fields.row.field_size", "size").c_str());
                printf("%s
", 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.

最佳回答

您的迭代中缺少一个关卡。 您需要重复含有多个同名子元素的元素 。

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.
}

树落下时酌情重复。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签