[Glade-users] libxml and treeview - how to read in more than one entries from an xml file
- From: atmadarshini_dd at yahoo.com (Atmadarshini devi dasi)
- Subject: [Glade-users] libxml and treeview - how to read in more than one entries from an xml file
- Date: Tue, 8 Apr 2008 05:43:08 -0700 (PDT)
Hi there
I am working with glade, gtktreeview and libxml. What i am trying to do is that from an xml file, I am trying
to put things into my treeview. My treeview for instance has three columns such as Lastname, Firstname,
cellphone.
my xml file looks like this:
<addressbook>
<contact>
<lastname> Grey </lastname>
<firstname> Jack </firstname>
<cellphone> 03445656565</cellphone>
</contact>
<contact>
<lastname> White</lastname>
<firstname>Mary </firstname>
<cellphone> 5656546456</cellphone>
</contact>
</addressbook>
Now I want for instance "Grey, Jack and 03445656565" to come into the first row and "White, Mary and
5656546456" to come into the second row.
I am able to get "Grey, Jack and 03445656565" into the first row but when i try for the second row, "Grey,
Jack and 03445656565" gets repeated instead of the second contact being read.
How do i go about doing that....I think i need some kind of loop but I am not sure. I am attaching my code
below. please tell me what code i can use to be able to access the other contacts in the xml file and then be
able to show them in the tree view. All help will be greatly appreciated.
/*this function belows gets the contacts from the addressbook*/
int addressbook_get_addresses_from_xml_file(char *filename)
{
xmlDocPtr doc;
xmlNodePtr cur, child;
address = (AddressBook*)malloc(sizeof(AddressBook));
if (filename == NULL)
filename = "addressbook.xml";
/* Parse our addressbook file */
if(!(doc = xmlParseFile(imsua_addpath(filename))))
{
g_warning("Error opening addressbook file: %s", imsua_addpath(filename));
xmlFreeDoc( doc );
return FALSE;
}
if(!(cur = xmlDocGetRootElement(doc)))
{
g_warning("Addressbook document has no root element");
xmlFreeDoc( doc );
return FALSE;
}
if(xmlStrcmp(cur->name, (const xmlChar *) "addressbook" ))
{
g_warning("XML document of the wrong type, root node != addressbook");
xmlFreeDoc( doc );
return FALSE;
}
cur = cur->xmlChildrenNode;
/* Traverse through document looking for our addressbook */
while(cur)
{
if (cur->type == XML_ELEMENT_NODE)
{
if(child = cur->xmlChildrenNode)
{
if(!xmlStrcmp(cur->name, (const xmlChar *)"contact")){
cur = cur->xmlChildrenNode;
while(cur) {
if(cur->type == XML_ELEMENT_NODE){
if(child = cur->xmlChildrenNode){
if(!xmlStrcmp(cur->name, (const xmlChar *)"lastname")){
strcpy(address->lastname, child->content);
printf("Last Nmae: %s\n", address->lastname);
}
if(!xmlStrcmp(cur->name, (const xmlChar *)"firstname")){
strcpy(address->firstname, child->content);
printf("First Name: %s\n", address->firstname);}
if(!xmlStrcmp(cur->name, (const xmlChar *)"homephone")){
//address->homephone = atoi(child->content);
strcpy(address->homephone, child->content);
printf("Home Phone: %s\n", address->homephone);}
if(!xmlStrcmp(cur->name, (const xmlChar *)"workphone")){
strcpy(address->workphone, child->content);
printf("Work Phone: %s\n", address->workphone);}
if(!xmlStrcmp(cur->name, (const xmlChar *)"cellphone")){
strcpy(address->cellphone, child->content);
printf("Cell Phone: %s\n", address->cellphone);}
if(!xmlStrcmp(cur->name, (const xmlChar *)"relationship"))
{
strcpy(address->relationship, child->content);
printf("Relationship: %s\n", address->relationship);}
}
}
cur = cur->next;
}
return TRUE;
}
}
}
cur = cur->next;
}
return TRUE;
}
/*this sets up the treeview*/
static void setup_tree_view (GtkWidget *treeview)
{
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Last Name", renderer,
"text", LASTNAME, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("First Name", renderer,
"text", FIRSTNAME, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Home Phone", renderer,
"text", HOMEPHONE, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Work Phone", renderer,
"text", WORKPHONE, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Cell Phone", renderer,
"text", CELLPHONE, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Relationship", renderer,
"text", RELATIONSHIP, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
}
/* Add the tree store */
static void
setup_tree_model (GtkWidget *treeview)
{
GtkListStore *store;
GtkWidget *list;
GtkTreeIter iter;
addressbook_get_addresses_from_xml_file("addressbook.xml");
store = gtk_list_store_new (COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,G_TYPE_STRING);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, LASTNAME, address->lastname, FIRSTNAME, address->firstname, HOMEPHONE,
address->homephone,WORKPHONE, address->workphone, CELLPHONE, address->cellphone, RELATIONSHIP,
address->relationship, -1);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, LASTNAME, address->lastname, FIRSTNAME, address->firstname, HOMEPHONE,
address->homephone,WORKPHONE, address->workphone, CELLPHONE, address->cellphone, RELATIONSHIP,
address->relationship, -1);
gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), GTK_TREE_MODEL (store));
g_object_unref (store);
}
/*the button to click in order to see the window with the treeview*/
void
on_view_button_clicked (GtkButton *button,
gpointer user_data)
{
GtkWidget *view_address_window, *treeview, *modify_button;
GladeXML *xml = NULL;
GNode *contact = NULL;
gchar *parsed[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
xml = glade_xml_new ("imsua.glade","view_address_window", NULL);
view_address_window = glade_xml_get_widget (xml, "view_address_window");
treeview = glade_xml_get_widget (xml, "treeview");
setup_tree_view (treeview);
setup_tree_model(treeview);
}
---------------------------------
You rock. That's why Blockbuster's offering you one month of Blockbuster Total Access, No Cost.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/glade-users/attachments/20080408/9e109149/attachment.html
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]