3
Using libxml2 to get the latest titles, descriptions, release dates,
4
and files paths from an audcast xml feed file
10
public errordomain XmlError
16
public class rssParser : GLib.Object {
17
//what signals will this class emit?
18
public signal void send_message(string message);
19
public signal void send_episode_info(string[] episode_data);
21
//what variable need to be persistant in scope?
22
List<string> episode_title;
23
List<string> episode_description;
24
List<string> episode_file;
25
List<string> episode_release_date;
28
//what happens when this class is constructed?
31
//initialisation, not instantiation since the parser is a static class
35
//we will have lists to store the data of the various shows
36
public void rssParser(){
38
this.episode_title = new List<string>();
39
this.episode_description = new List<string>();
40
this.episode_file = new List<string>();
41
this.episode_release_date = new List<string>();
42
this.episode_count = 0;
45
private void parse_item_node( Xml.Node* the_node )
47
//get the children of the root node as a node
48
Xml.Node* node_children = the_node->children;
49
Xml.Node* iter = node_children->next;
51
string iter_node_name;
53
string description = "";
55
string release_date = "";
56
//this is an episode, increment the episode count
60
//spaces btw. tags are also nodes, discard them
61
if (iter->type != ElementType.ELEMENT_NODE)
65
iter_node_name = iter->name;
67
switch(iter_node_name)
70
title = iter->get_content();
74
description = iter->get_content();
77
file = iter->get_prop("url");
80
release_date = iter->get_content();
87
//send the data as a signal
88
string[] data = new string[4];
93
send_episode_info(data);
94
this.episode_title.append(title);
95
this.episode_description.append(description);
96
this.episode_file.append(file);
97
this.episode_release_date.append(release_date);
100
public void print_episode_info()
106
for ( int i = 0; i<this.episode_count;i++)
108
title = this.episode_title.nth_data(i);
109
description = this.episode_description.nth_data(i);
110
file = this.episode_file.nth_data(i);
111
release_date = this.episode_release_date.nth_data(i);
112
stdout.printf("%s\n",title);
113
stdout.printf("%s\n",file);
114
stdout.printf("%s\n",release_date);
116
//stdout.printf("%s\n\n",description);
120
public void parse_file(string file) throws XmlError
123
send_message("parsing "+file);
125
Xml.Doc* xml_doc = Parser.parse_file(file);
126
if (xml_doc == null) {
127
send_message(file+" is not readable");
128
throw new XmlError.FILE_NOT_FOUND (file+" is not readable");
131
//get the root node. notice the dereferencing operator -> instead of .
132
Xml.Node* root_node = xml_doc->get_root_element();
134
if (root_node == null) {
135
//free the document manually before throwing because the garbage collector can't work on pointers
137
send_message(file+" is empty");
138
throw new XmlError.XML_DOCUMENT_EMPTY (file + " is empty");
141
//get the children of the root node as a node
142
Xml.Node* root_children = root_node->children;
143
Xml.Node* iter = root_children->next;
145
string iter_node_name;
146
//rss should be the top node
147
if(iter->name=="channel")
149
//get the children of the channel
150
iter = iter->children;
151
//get the first node of the channel children
153
//loop through the channel children
156
//spaces btw. tags are also nodes, discard them
157
if (iter->type != ElementType.ELEMENT_NODE)
161
iter_node_name = iter->name;
162
if(iter_node_name=="item")
164
parse_item_node(iter);
170
//the first node under root is not the channel, what should I do?
176
public void clean_up()
178
//do the parser cleanup to free the used memory