开发者

how to convert c string to xml using standard c++?

I would like to know is there any way to generate xml schema from a c string by using stander c++? In my current application there is a server which sends comma separated string by a socket. That is cumbersome to parse date. I would like to extend this to convert this开发者_如何学Go string into a standard XML format. So that I could easily parse data from client side.

c string:- Temperature,low:20,high:30,current:24

xml:-

<temperature>
  <low>20</low>
  <high>30</high>
  <current>24</current>
</temperature>    

Please provide some link or example to study more. Thanks!


C++ doesn't have a XML parser as part of the standard library. To do this you need a 3rd party library. MSXML and Xerces are two that I have used. While Xerces is a little more cumbersome to set up I have to say it is much nicer of the two.


Basically you have two separate steps:

  1. Parse the String and get the information you want out of it
  2. Write the XML

For Step 1. I would suggest using some regular expression library, e.g. Boost.Regex.

As soon as you have the information parsed, you can simply write the XML into a string using standard streams like

std::stringstream s;
s << "<temperature><low>" << low << "</low><high>" << high << "</high>";
s << "<current>" << current << "</current>";
s << "</temperature>";

and use the resulting string with s.str().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜