-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_rss2.c
77 lines (69 loc) · 2.35 KB
/
read_rss2.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <libxml/xmlreader.h>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
void parseXML(const char* filename) {
xmlTextReaderPtr reader;
int ret;
reader = xmlReaderForFile(filename, NULL, 0);
if (reader != NULL) {
ret = xmlTextReaderRead(reader);
while (ret == 1) {
if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
// Check if the element is "title"
if (xmlStrEqual(xmlTextReaderConstName(reader), (const xmlChar*)"title")) {
// Get the value of the "title" element
xmlChar *value = xmlTextReaderReadString(reader);
printf("%s\n",(const char*)value);
xmlFree(value);
}
}
// Check if the current node is an element
if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
// Check if the element is "description"
if (xmlStrEqual(xmlTextReaderConstName(reader), (const xmlChar*)"description")) {
// Get the value of the "description" element
xmlChar *value = xmlTextReaderReadString(reader);
printf("%s\n",(const char*)value);
xmlFree(value);
}
}
ret = xmlTextReaderRead(reader);
}
xmlFreeTextReader(reader);
if (ret != 0) {
printf("Error parsing XML\n");
}
} else {
printf("Unable to open %s\n", filename);
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s RSS_URL\n", argv[0]);
return 1;
}
CURL *curl;
FILE *fp;
CURLcode res;
char *url = argv[1];
char outfilename[FILENAME_MAX] = "rss.xml";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
} else {
printf("Error initializing cURL\n");
}
parseXML(outfilename);
return 0;
}