【问题标题】:enum input from xml to c++ program using boost::property_tree使用 boost::property_tree 从 xml 到 c++ 程序的枚举输入
【发布时间】:2013-12-28 16:15:49
【问题描述】:

如何使用boost::property_tree 库从 XML 文件中读取枚举类?

我想避免将其作为字符串读取并将该字符串映射到我程序中的枚举类。

【问题讨论】:

    标签: c++ boost c++11 boost-propertytree enum-class


    【解决方案1】:

    读取property_tree/xml_parser.hpp 头文件。这很简单。

    这里是最重要的部分,都在 boost::property_tree::xml_parser 命名空间中:

    /**
         * Reads XML from an input stream and translates it to property tree.
         * @note Clears existing contents of property tree.  In case of error the
         *       property tree unmodified.
         * @note XML attributes are placed under keys named @c \<xmlattr\>.
         * @throw xml_parser_error In case of error deserializing the property tree.
         * @param stream Stream from which to read in the property tree.
         * @param[out] pt The property tree to populate.
         * @param flags Flags controlling the behaviour of the parser.
         *              The following flags are supported:
         * @li @c no_concat_text -- Prevents concatenation of text nodes into
         *                          datastring of property tree.  Puts them in
         *                          separate @c \<xmltext\> strings instead.
         * @li @c no_comments -- Skip XML comments.
         * @li @c trim_whitespace -- Trim leading and trailing whitespace from text,
         *                           and collapse sequences of whitespace.
         */
        template<class Ptree>
        void read_xml(std::basic_istream<
                          typename Ptree::key_type::value_type
                      > &stream,
                      Ptree &pt,
                      int flags = 0)
        {
            read_xml_internal(stream, pt, flags, std::string());
        }
    
        /**
         * Reads XML from a file using the given locale and translates it to
         * property tree.
         * @note Clears existing contents of property tree.  In case of error the
         *       property tree unmodified.
         * @note XML attributes are placed under keys named @c \<xmlattr\>.
         * @throw xml_parser_error In case of error deserializing the property tree.
         * @param filename The file from which to read in the property tree.
         * @param[out] pt The property tree to populate.
         * @param flags Flags controlling the bahviour of the parser.
         *              The following flags are supported:
         * @li @c no_concat_text -- Prevents concatenation of text nodes into
         *                          datastring of property tree.  Puts them in
         *                          separate @c \<xmltext\> strings instead.
         * @li @c no_comments -- Skip XML comments.
         * @param loc The locale to use when reading in the file contents.
         */
        template<class Ptree>
        void read_xml(const std::string &filename,
                      Ptree &pt,
                      int flags = 0,
                      const std::locale &loc = std::locale())
        {
            BOOST_ASSERT(validate_flags(flags));
            std::basic_ifstream<typename Ptree::key_type::value_type>
                stream(filename.c_str());
            if (!stream)
                BOOST_PROPERTY_TREE_THROW(xml_parser_error(
                    "cannot open file", filename, 0));
            stream.imbue(loc);
            read_xml_internal(stream, pt, flags, filename);
        }
    

    【讨论】:

    • 您能否为无法访问链接的人详细说明?
    • 那里。再简单不过了。
    • 感谢您的帮助。我意识到我问错了问题并创建了一个新问题here。
    猜你喜欢
    • 2013-12-28
    • 2019-02-11
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多