使用字符串函数在 C 中解析 xml
我有一个 xml 文件,我必须读取并解析该文件才能找出一个值。
我在文件中两个位置的标记中提到了这个值,如下所示:
<length>xx</length>
<length type = "float">yy</length>
我需要提取 xx 和 yy。我正在使用简单的字符串函数(由于大小限制,我无法使用 xml 解析器)。
哪些字符串函数可以帮助我提取 xx 和 yy?
我在这些线上尝试了 strtok() 但没有成功......:
fp = fopen( "trial.xml", "r" );
if(fp == NULL){
perror("file missing");
}
while (fgets (buffer, sizeof (buffer), fp) != NULL) {
char *p;
p = strstr(buffer, "<length");
if(p != NULL){
printf("p = %s\n", p);
p = strtok (p, "<>");
printf("strtok 1, p = %s\n", p);
p = strtok (NULL, "<>");
printf("p = %s\n", p);
}
I have an xml file which I have to read and parse to find out one value.
I have this value mentioned within tags at two places in the file as follows:
<length>xx</length>
<length type = "float">yy</length>
I need to extract xx and yy. I am using simple string functions(owing to size restrictions I cant use xml Parsers).
What string functions would help me extract xx and yy?
I tried strtok() on these lines but without success..:
fp = fopen( "trial.xml", "r" );
if(fp == NULL){
perror("file missing");
}
while (fgets (buffer, sizeof (buffer), fp) != NULL) {
char *p;
p = strstr(buffer, "<length");
if(p != NULL){
printf("p = %s\n", p);
p = strtok (p, "<>");
printf("strtok 1, p = %s\n", p);
p = strtok (NULL, "<>");
printf("p = %s\n", p);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 TinyXML 进行解析。
如果你想使用暴力...
I would suggest using TinyXML to do your parsing.
If you want to use brute force...