簡體   English   中英

使用boost屬性樹解析XML

[英]Parse XML with boost property tree

我有以下XML文件,我想使用以下結構存儲它。

數據結構:

    struct transitions
    {
     string oldstate;
     string event;
     string newstate;
    };

    struct XML_Diagram
    {
     string diag_name;
     string diag_defaultstate;
     list<string> diag_states;
     list<string> diag_events;
     list<transitions> diag_transitions;
    };

xml文件:

    <diagram>
      <diagname>DiagaX</diagname>
      <states>
         <state>A</state>
         .............       
      </states>
      <events>
          <event>ev1</event>
          .................
      </events>
      <defaultstate>A</defaultstate>
      <transitions>
          <transition>
              <oldstate>A</oldstate>
              <event>ev1</event>
              <newstate>B</newstate>
          </transition>
          <transition>
              <oldstate>B</oldstate>
              <event>ev2</event>
              <newstate>C</newstate>
          </transition>
          .........................
      </transitions>
    </diagram>

我很清楚如何訪問diagram.states。 我可以使用以下代碼執行此操作:

    using boost::property_tree::ptree;
    ptree pt;

    // Get diagram states
    BOOST_FOREACH(ptree::value_type &v, pt.get_child("diagram.states"))
    {
       diag_states.push_back(v.second.data());
    }

我不清楚的是如何從圖表中訪問數據.transitions.transition?

我的問題是我在文檔中找不到有關如何解析具有多個級別的更復雜的xml文件的任何示例。

這個有用的實用程序函數遍歷並漂亮打印整個屬性樹:

using boost::property_tree::ptree;

std::string q(const std::string& s) { return "\"" + s + "\""; }

void print_tree(const ptree& pt, int level)
{
    const std::string sep(2 * level, ' ');
    BOOST_FOREACH(const ptree::value_type &v, pt) {
        std::cout
            << sep
            << q(v.first) << " : " << q(v.second.data()) << "\n";
        print_tree(v.second, level + 1);
    }
}

void print_tree(const ptree& pt) { print_tree(pt, 0); }

v.second值是樹本身,可以使用常用的get方法訪問。 例如,可以像這樣訪問和打印轉換:

using std::string;

void print_transitions(const ptree& pt)
{
    BOOST_FOREACH(
        const ptree::value_type &v,
        pt.get_child("diagram.transitions"))
    {
        const ptree& child = v.second;
        std::cout
            << "Event "
            << child.get<string>("event")
            << " in state "
            << child.get<string>("oldstate")
            << " leads to state "
            << child.get<string>("newstate")
            << "\n";
    }
}

這是另一個如何使用屬性打印ptree示例:

namespace pt = boost::property_tree;

// worker
typedef std::pair<const pt::ptree&, unsigned> tree_printer;

// empty ptree helper
const pt::ptree& empty_ptree()
{
    static pt::ptree t;
    return t;
}


std::ostream& operator <<(std::ostream& os, const tree_printer& p)
{
    const pt::ptree& tree = p.first;

    if(tree.empty()) return os;

    const std::string indent(p.second, ' ');

    BOOST_FOREACH(const pt::ptree::value_type& v, tree)
    {
        const std::string& nodeName = v.first;

        if(nodeName == "<xmlattr>") continue;

        os << indent << nodeName;
        const pt::ptree& attributes =
            v.second.get_child("<xmlattr>", empty_ptree());

        if(!attributes.empty())
        {
            os << " [ ";
            BOOST_FOREACH(const pt::ptree::value_type& attr, attributes)
            {
                const std::string& attrName = attr.first;
                const std::string& attrVal = attr.second.data();

                os << attrName << " = '" << attrVal << "'; ";
            }
            os << "]";
        }
        os << "\n";

        const pt::ptree& childNode = v.second;
        os << tree_printer(childNode, p.second + 1);
    }

    return os;
}


std::ostream& operator <<(std::ostream& os, const pt::ptree& tree)
{
    os << tree_printer(tree, 0);

    return os;
}

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM