How are attributes parsed in Boost.PropertyTree?
Asked Answered
O

2

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?

Onus answered 11/9, 2010 at 9:18 Comment(1)
Full code example here: https://mcmap.net/q/581779/-parsing-xml-attributes-with-boostEcclesiasticism
R
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.

Retouch answered 11/9, 2010 at 13:55 Comment(3)
Thank you very much. How do I access it exactly? I would really like to see an examle.Onus
Will BOOST_FOREACH(ptree::value_type &v, pt.get_child("widget.xmlattr")) attributes.insert(std::make_pair(v.first.data(), v.second.data()) do?Onus
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.Chandelier
A
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!

Aporia answered 10/10, 2011 at 7:58 Comment(1)
<mode fullscreen="true">mod xxx</mod> does not appear to be well-formed xml.Porcia
R
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.

Retouch answered 11/9, 2010 at 13:55 Comment(3)
Thank you very much. How do I access it exactly? I would really like to see an examle.Onus
Will BOOST_FOREACH(ptree::value_type &v, pt.get_child("widget.xmlattr")) attributes.insert(std::make_pair(v.first.data(), v.second.data()) do?Onus
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.Chandelier

© 2022 - 2024 — McMap. All rights reserved.