English 中文(简体)
如何处置这些财产。
原标题:Boost Property_Tree iterators, how to handle them?

我很抱歉,我就前面的同一专题提出了一个问题,但我的问题涉及所描述的另一个方面()。 如何刺激......

研究了以下法典:

#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/algorithm/string/trim.hpp>
int main(int argc, char** argv) {
     using boost::property_tree::ptree;
     ptree pt;
     read_xml("try.xml", pt);
     ptree::const_iterator end = pt.end();
     for (ptree::const_iterator it = pt.begin(); it != end; it++)
           std::cout << "Here " << it->? << std::endl;
}

诚然,正如我在前面提到的问题中所告诉的那样,在波斯特,有可能在<条码>property_上使用激光器,但我不知道它是什么类型,我可以使用什么方法或财产。

我假设,它必须是另一个星号,即“德国”、“德国”、“德国”或“代表另一个xm等级的”再行(如果我想要的话),但有关这种文件正是这样。 我并不知道为什么、但还是要到底,我真的希望避免这种做法。

So getting to my question here: Once getting the iterator on a ptree, how can I access node name, value, parameters (a node in a xml file)? Thankyou

最佳回答

我同意安德里,认为财产——树木——的文件至少是极低的。 我需要tree树,在不同的环境中装上相同的物体,并难以确定该装置的变异性、其回报的类型、以及它是否将停留在物体上,或者穿过像BFS这样的每一个 no子。 最后,我设法处理一个类似于以下案件:

场所档案:

<object1>
    <enable>true</enable>
    <label>hello</label>
</object1>
<object2>
    <enable>false</enable>
    <label>goodbye</label>
</object2>

首先,我增加了我的物体的构造者,可以先入树。 注:采用缺省办法避免出现故障时的例外情况的Im :

object::object(const boost::property_tree::ptree &pt_)
{
    enable = pt_.get<bool>("enable", true); // usage is: get<type>(path, default)
    label  = pt_.get<std::string>("label", "empty");
}

最后,以下法典对两种物体都装上,并将其放在地图上:

std::map<std::string, my_object> objects_map;

// parse settings file and add loggers
if(filesystem::exists(logger_settings_file))
{
    boost::property_tree::ptree pt;

    read_xml(logger_settings_file, pt);
    BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt)
    {
        objects_map[v.first] = my_object(v.second);
    }
}

因此,回答我自己的问题:

  • The iterator iterates over the settings file without descending into lower levels. Running the above code you will find that the loop iterates twice - one time for each object in the XML file.
  • The iterator returns a value_type object which resembles a pair, and has the first and second accessors. v.first is an std::string holding the parent node (in my case "object1", "object2"), and v.second is a boost::property_tree::ptree, which can be used to parse the fields of the object.
问题回答

print树:

void print(boost::property_tree::ptree const& pt)
{
    using boost::property_tree::ptree;
    ptree::const_iterator end = pt.end();
    for (ptree::const_iterator it = pt.begin(); it != end; ++it) {
        std::cout << it->first << ": " << it->second.get_value<std::string>() << std::endl;
        print(it->second);
    }
}

您应事先了解投入物档案。

Boo树不是一种一般文件。 它将使数据趋于平流并便于查阅,但必须以人工定位。

I don t know there are method to navigate whole document, but if you only need the properties of your own file you can do it with very simple code.

http://live.boost.org/doc/libs/1_42_0/doc/html/boost_propertytree/accessing.html” rel=“nofollow”>boost 文件:

(1) 投掷版本(目标):

ptree pt;
/* ... */
float v = pt.get<float>("a.path.to.float.value");

(2) 违约价值(目标):

ptree pt;
/* ... */
float v = pt.get("a.path.to.float.value", -1.f);

3) 任择文本(目标:选择):

ptree pt;
/* ... */
boost::optional<float> v = pt.get_optional<float>("a.path.to.float.value");




相关问题
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?

热门标签