ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

In C++ you don't have introspection for messages, i.e. you need to know the message types at compile time. In theory, it would be possible to deserialize messages into something dynamic such as a stl map over strings and boost::any or something but I don't know of any implementation.

Python is much easier because it's a dynamic language. You can import messages at run-time and you can introspect instances of any class at run-time. Consider the following example:

import roslib; roslib.load_manifest('geometry_msgs')
import geometry_msgs.msg

p = geometry_msgs.msg.Vector3(1, 2, 3)
print p.__slots__
print p.__slot_types
print p.__get_attribute__('x')

It shows how to list all slots, i.e. member variables of a ros message, how to get the types of all slots and how to get the value of a slot. With that it should be pretty easy to write something generic to create an xml representation of messages.

In C++ you don't have introspection for messages, i.e. you need to know the message types at compile time. In theory, it would be possible to deserialize messages into something dynamic such as a stl map over strings and boost::any or something but I don't know of any implementation.

Python is much easier because it's a dynamic language. You can import messages at run-time and you can introspect instances of any class at run-time. Consider the following example:

import roslib; roslib.load_manifest('geometry_msgs')
import geometry_msgs.msg

p = geometry_msgs.msg.Vector3(1, 2, 3)
print p.__slots__
print p.__slot_types
p._slot_types
print p.__get_attribute__('x')
p.__getattribute__('x')

It shows how to list all slots, i.e. member variables of a ros message, how to get the types of all slots and how to get the value of a slot. With that it should be pretty easy to write something generic to create an xml representation of messages.