Comment detail

XMLから情報を取り出す (Nested Flatten)
data変数という扱いでよくて助かった。 <lastBuildDate>に属性、空白が入らないことが前提になります。複数回出現しても表示できるようになっています。 あ、ネストされてたら変な表示になりそう。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char *data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                 "<rss version=\"2.0\"><channel><title>どう書く?org 新着コメン
</title><link>http://ja.doukaku.org/</link><description>どう書く?orgに最近投
稿されたコード</description><language>ja</language><lastBuildDate>Fri, 13 Jul 20
07 11:17:01 -0000</lastBuildDate><item><title>ココサブ's comment on ウィンドウの
表示"
                 "</title><link>http://ja.doukaku.org/comment/744/</link><descri
ption>"
                 "&lt;a href=\"http://ja.doukaku.org/7/\"&gt;ウィンドウの表示&lt
;/a&gt;"
                 "&lt;hr&gt;";

    char *head, *tail;
    char *result;
    char *data_p;
    int  result_len;

    data_p = data;
    while (1) {
        head = strstr(data_p, "<lastBuildDate>");
        if (head == NULL) break;
        tail = strstr(head, "</lastBuildDate>");
        if (tail == NULL) break;

        /* 答えの長さを求める         */
        result_len = (tail - head) + strlen("</lastBuildDate>");

        result = malloc((result_len + 1) * sizeof(char));
        strncpy(result, head, result_len);
        result[result_len] = '\0';
        printf("%s\n", result);
        free(result);

        data_p = tail + strlen("</lastBuildDate>");
    }

    return 0;
}

Index

Feed

Other

Link

Pathtraq

loading...