8

次の行に沿った XML 構造があります。

<root>
 <SomeElement>
  <AnotherElement>
   <ElementIWant x="1" y="1"/>
  </AnotherElement>
 </SomeElement>
 <SomeElement>
  <AnotherElement>
   <ElementIWant x="1" y="1"/>
   <ElementIWant x="2" y="1"/>
   <ElementIWant x="3" y="1"/>
  </AnotherElement>
 </SomeElement>
</root>

これは に読み込まれます。1..多くboost::property_treeのがあり、その要素内の任意の深さに 1..多くの が存在する可能性があります <SomeElement> <ElementIWant>

<ElementIWant>ドキュメントに表示される順序で直接 (単一のループで)反復処理する方法はありますか?

equal_range を見てきました

void iterateOverPoints()
{
     const char* test = 
     "<?xml version=\"1.0\" encoding=\"utf-8\"?><root>"
      "<SomeElement>"
       "<AnotherElement>"
        "<ElementIWant x=\"1\" y=\"1\"/>"
       "</AnotherElement>"
      "</SomeElement>"
      "<SomeElement>"
       "<AnotherElement>"
        "<ElementIWant x=\"1\" y=\"1\"/>"
        "<ElementIWant x=\"2\" y=\"1\"/>"
        "<ElementIWant x=\"3\" y=\"1\"/>"
       "</AnotherElement>"
      "</SomeElement>"
    "</root>";

    boost::property_tree::ptree message;
    std::istringstream toParse(test); 
    boost::property_tree::read_xml(toParse,result_tree);

    //Now we need to locate the point elements and set the x/y accordingly.
    std::pair< boost::property_tree::ptree::const_assoc_iterator,
               boost::property_tree::ptree::const_assoc_iterator > result =
         message.equal_range("ElementIWant");

    for( boost::property_tree::ptree::const_assoc_iterator it = result.first; 
           it != result.second; ++it )
    {
        std::cout  << it->first << " : ";
        const boost::property_tree::ptree& x = it->second.get_child( "<xmlattr>.x" );
        const boost::property_tree::ptree& y = it->second.get_child( "<xmlattr>.y" );
        std::cout << x.get_value<int>() << "," << y.get_value<int>() << "\n";
    }

    return;
}

ただし、ノードを返すのに失敗しているようです (equal_range が提供されたツリー ノードのレベルで機能するためだと思われます)。

4

1 に答える 1

6

すべての要素を直接反復処理することはできません。ドキュメントによると

ツリー全体を反復処理する方法はありません。

これで、再帰を使用し、各レベルで STL アルゴリズムを適用してそれを模倣できます。以下のサンプルでは、​​単一のループでこれを行うという要件には適合しませんが、機能します。

template <typename InputIt, typename OutputIt, typename Compare>
void collect(InputIt first, InputIt last, OutputIt dest, Compare comp)
{
    typedef typename std::iterator_traits<InputIt>::reference reference;

    std::copy_if (
        first, last, dest,
        [comp] (reference what) { return comp(what.first); });

    std::for_each (
        first, last,
        [dest, comp] (reference what) { collect(what.second.begin(), what.second.end(), dest, comp); });
}


std::vector<std::pair<std::string, ptree>> match;

collect(
    xml.begin (), xml.end (), std::back_inserter(match),
    [] (const std::string& key) { return key == "ElementIWant"; });

for (auto pair: match)
{
     std::cout << pair.first << std::endl;
}

「完全に」再帰的で、出現順序を保持するバージョンを次に示します。

template <typename InputIt, typename OutputIt, typename Compare>
void collect_recursive(InputIt first, InputIt last, OutputIt dest, Compare comp)
{
    typedef typename std::iterator_traits<InputIt>::reference reference;

    if (first == last)
    {
        return;
    }

    auto begin = first->second.begin ();
    auto end = first->second.end ();

    if (begin != end)
    {
        collect_recursive (begin, end, dest, comp);
    }

    if (comp (first->first))
    {
        dest = *first;
    }

    collect_recursive (++first, last, dest, comp);
}
于 2012-12-28T13:04:05.360 に答える