开发者

How to parse a value from an XML tag in C language?

I'm getting this string as an input 开发者_开发百科"<operator value=1/>". How in C can I parse out this string to get the "1" back?


If you know that that's exactly the type of string you'll always get, you can just use sscanf():

int value;
if(sscanf(theString, "<operator value=%d/>", &value) == 1)
    ;  // parsing succeeded, use value
else
    ;  // parsing failed

If you're going to be receiving more complex input, I recommend using an XML parsing library such as expat or TinyXML (requires C++).


i am assuming your parsing the string without using any library and the string may not be legal. I once encountered this problem when parsing sgml.

there are a few ways to do this. You could use strstok to split the string to find the value 1, then convert it using atoi.

I would recommend you use a regex library to extract the string. It would handle more boundary cases.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜