GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 23, 2014 8:44 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: reading more than one entry from an xml file into a treeview
PostPosted: Tue Apr 08, 2008 12:51 pm 
Offline
Familiar Face

Joined: Tue Apr 01, 2008 11:56 am
Posts: 6
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.

[code=]/*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);

}
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 3:40 am 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
I would guess that it might be because you are using the same xmlNodePtr (cur) to go through every item in your xml file.
You could try using an array, like this:
Code:
xmlNodePtr cur[3];

and then use a different cur[n] item instead of reusing cur.
Also, it would be easier to use xml properties instead of multiple elements,
for example:
Code:
<addressbook>
<contact lastname="Grey" firstname="Jack" cellphone="03445656565"/>
<contact lastname="White" firstname="Mary" cellphone="5656546456"/>
</addressbook>

then use
Code:
if(!xmlStrcmp(cur[n]->name, (xmlChar*)"contact"))
{
    strcpy(address->lastname, (char*)xmlGetProp(cur[n], (xmlChar*)"lastname"));
    strcpy(address->firstname, (char*)xmlGetProp(cur[n], (xmlChar*)"firstname"));
    /* and so on */
}


Hope this helps you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group