GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

reading more than one entry from an xml file into a treeview

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
atma64
Familiar Face


Joined: 01 Apr 2008
Posts: 6

PostPosted: Tue Apr 08, 2008 12:51 pm    Post subject: reading more than one entry from an xml file into a treeview Reply with quote

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: (Plaintext)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*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);

}
Back to top
dreblen
Familiar Face


Joined: 14 Jun 2007
Posts: 38

PostPosted: Thu Apr 10, 2008 3:40 am    Post subject: Reply with quote

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: (C)
1
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: (XML/HTML)
1
2
3
4
<addressbook>
<contact
lastname="Grey" firstname="Jack" cellphone="03445656565"/>
<contact
lastname="White" firstname="Mary" cellphone="5656546456"/>
</addressbook>

then use
Code: (C)
1
2
3
4
5
6
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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP