24

Say I have this XML format:

<Widget type="SomeWidget" name="foo">
   <Event name="onmouseover">
      dostuff();
   </Event>
</Widget>

How do I read the attributes using Boost.PropertyTree?

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
the_drow
  • 18,571
  • 25
  • 126
  • 193

2 Answers2

61

If xml has such content:

<mode fullscreen="true">mode xxx</mode>

Use boost::property code:

get<string>("mode.<xmlattr>.fullscreen") 

Oh yeah, it's ugly!

Kevin Zhao
  • 611
  • 1
  • 5
  • 2
12

If your problem is to get attributes:

The attributes of an XML element are stored in the subkey . There is one child node per attribute in the attribute node. Existence of the node is not guaranteed or necessary when there are no attributes.

From the doc http://www.boost.org/doc/libs/1_44_0/doc/html/boost_propertytree/parsers.html#boost_propertytree.parsers.xml_parser

So just get them from the <xmlattr> key in the path.

Nerpson
  • 382
  • 2
  • 13
Klaim
  • 67,274
  • 36
  • 133
  • 188
  • Thank you very much. How do I access it exactly? I would really like to see an examle. – the_drow Sep 12 '10 at 08:21
  • 1
    Will `BOOST_FOREACH(ptree::value_type &v, pt.get_child("widget.xmlattr")) attributes.insert(std::make_pair(v.first.data(), v.second.data())` do? – the_drow Sep 12 '10 at 11:31
  • I would love to see a sample that doesn't use BOOST_FOREACH. How about just pure C++ 98? If not, C++ 11 would be okay too, but not ideal. Part of learning a library is seeing the datatypes in use and auto hides the details of learning. – Mitch Sep 27 '16 at 20:36