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 

Need help to retrieve all elements and attributes from xml

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


Joined: 22 Nov 2006
Posts: 12

PostPosted: Tue Nov 28, 2006 8:13 pm    Post subject: Need help to retrieve all elements and attributes from xml Reply with quote

Hi, all.

I am trying to use libxml2 to retrieve all the elements and attributes from a xml file. Through the tutorial in xmlsoft.org, I am able to compile the example code to retrieve A certain element or attribute. Now I want to know how to retrieve ALL elements or attributes in this xml file. Or say how I can walk through the whole xml file and read its content.

Any idea or link for my question?

I know this is not a actual GTK+ question, but waht I wan to do just to read the content from a xml file and load to the CLIST in GTK+. I hope the guru here can help me.
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 387
Location: Fairfax, Virginia

PostPosted: Wed Nov 29, 2006 2:41 am    Post subject: Reply with quote

It's been a while since I've used libxml2, but if you just need to parse small XML files, GLib provides an XML parser. API docs are available at: http://developer.gnome.org/doc/API/2.0/glib/glib-Simple-XML-Subset-Parser.html
Back to top
kingsz1
Familiar Face


Joined: 22 Nov 2006
Posts: 12

PostPosted: Wed Nov 29, 2006 9:49 pm    Post subject: Thank you. Reply with quote

Thank you indeed.

Here is the xml sample from xmlsoft.org
Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

<?xml version="1.0"?>
<story>
  <storyinfo>
    <author>John Fleck</author>
    <datewritten>June 2, 2002</datewritten>
    <keyword>example keyword</keyword>
  </storyinfo>
  <body>
    <headline>This is the headline</headline>
    <para>This is the body text.</para>
  </body>
</story>


Here is the code portion from xmlsoft.org with my modification to display all elements inside:
Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

void
parseStory (xmlDocPtr doc, xmlNodePtr cur) {

    xmlChar *key;
    cur = cur->xmlChildrenNode;
    while (cur != NULL) {
/*         if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) {
            key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
            printf("keyword: %s\n", key);
            xmlFree(key);
         }
*/
    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
    printf("Element:Name %s\n", cur->name);
    printf("Element Value: %s\n", key);
    cur = cur->next;
    }
    return;
}


Here is the output:


What's wrong with my code?
[/img]
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