Ask Your Question
1

How to convert XmlRpc::XmlRpcValue into string?

asked 2011-12-13 04:45:20 -0500

Felix Tr gravatar image

updated 2011-12-14 01:32:40 -0500

Hello All,

I am trying to extract topic name as strings from a "yaml" file. The goal is depicted here:

http://answers.ros.org/question/3289/how-to-write-and-parse-yaml-file-for-ros

I think that I have succeeded to receive the list as XmlRpc::XmlRpcValue class, but I am not succeeding to convert this to a string format.

I have tried to do it in two different ways:

    for (int32_t i = 0; i < my_list.size(); ++i) 
    {
        printf ( "Topic %d is: %s.\n", i, static_cast<std::string>(my_list[i]) );
    }

if ( my_list.stringFromXml ( topicsList, &offset ) == TRUE )
{
        printf ("\"topic_list = \"%s", topicsList );
    }

The first way doesn´t compiles, and the second doesn´t run...

The two relevant links, I have found, that are connected to this problem are:

  1. http://www.ros.org/wiki/roscpp/Overview/Parameter%20Server (roll till the end of the page).
  2. http://xmlrpcpp.sourceforge.net/doc/classXmlRpc_1_1XmlRpcValue.html

Both of them doesn´t really help me.

Here is also the code for getting my_list:

ros::NodeHandle nh;
XmlRpc::XmlRpcValue my_list;
nh.getParam("topic_list", my_list);
if ( my_list.stringFromXml ( topicsList, &offset ) == TRUE )
{
        printf ("\"topic_list = \"%s", topicsList );
    }

While topic_list is the name of the list in "yaml" file:

topic_list:
- /bb2/left/image_rect
- /bb2/right/image_rect

Thanks in advance, Felix.

edit retag flag offensive close merge delete

Comments

What exactly is the compilation error in the first case? Can you also include the code for getting my_list?
arebgun gravatar imagearebgun ( 2011-12-13 05:03:57 -0500 )edit
See my next post in this page please. Thank you.
Felix Tr gravatar imageFelix Tr ( 2011-12-13 05:16:20 -0500 )edit

2 Answers

Sort by » oldest newest most voted
4

answered 2011-12-13 05:19:03 -0500

DimitriProsser gravatar image

updated 2011-12-14 01:59:46 -0500

I think the main problem is that printf's %s formatter expects a C-style string, not a C++ string. All you'd have to do to fix this is:

printf ( "Topic %d is: %s.\n", i, static_cast<std::string>(my_list[i]).c_str() );

Note the addition of the .c_str() function call.

Also, you might want to change printf to ROS_INFO. All of the formatting is the same, but it has cool ROS functionality.

EDIT: Here's a quick explanation. Since printf is the output format used in C, not in C++, it expects that you'd be using C-style data structures. The %s formatter in C expects that you will pass in a const char*, which is C's representation of a string - an array of characters. In C++, however, the std::string data structure is more complex than a const char* and is its own type entirely. It's essentially the same as trying to use a double with %s. It's the wrong datatype, and therefore will not work. The .c_str() method of a C++ std::string returns a const char* of the string so that you can use it with C functions.

The block of code static_cast<std::string>(my_list[i]) returns a std::string, and thus you cannot use it with %s. static_cast<std::string>(my_list[i]) is one of C++'s forms of casting my_list[i] to an std::string. static_cast<double>(my_list[i]) would cast it to a double.

edit flag offensive delete link more

Comments

Dmitri, THANKS A LOT for this answer, it works :)!!! I will of course read about it more, but if you have time, can you explain your answer here (decompose into parts). I am writing mostly in C, so I am not sharp enough in C++. Thanks.
Felix Tr gravatar imageFelix Tr ( 2011-12-14 01:43:35 -0500 )edit
Thanks again :)
Felix Tr gravatar imageFelix Tr ( 2011-12-14 02:11:42 -0500 )edit
0

answered 2011-12-13 05:48:29 -0500

dornhege gravatar image

The code should work.

Can you verify this assertion: ROS_ASSERT(my_list.getType() == XmlRpc::XmlRpcValue::TypeArray);

Maybe you are not requesting the param in the right scope.

edit flag offensive delete link more

Comments

There are two problems with that: 1. I don´t need the assertion. 2. It doesn´t compiles that way. Anyway, thanks a lot for your answers here, and anyway with the help from this forum, I have succeeded to solve the problem :).
Felix Tr gravatar imageFelix Tr ( 2011-12-14 02:20:47 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-12-13 04:45:20 -0500

Seen: 4,793 times

Last updated: Dec 14 '11