Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
C - libxml2 parsing example libxml       Share
2007-11-03 |  Blueball  | Viewed: 1939  |    0

/*
<ROOT>
       <CHILD  id="attribute"  value="1"  />
       <CHILD  id="attribute"  value="1"  />
</ROOT>        

compile:

//  g++  filename.cpp  -o  exefilename  `xml2-config  --cflags  --libs`

*/

//  xml  parsing  example
//  Reading  element  from  xml  file

#include   <stdio.h>
#include   <string.h>
#include   <stdlib.h>
#include   <libxml/xmlmemory.h>
#include   <libxml/parser.h>

void getReference (xmlDocPtr doc, xmlNodePtr cur) {
    xmlChar *uri;

    xmlChar *value;

    cur = cur->xmlChildrenNode;

    while (cur != NULL) {

        
if ((!xmlStrcmp(cur->name, (const xmlChar *)"setting"))) {
            uri = xmlGetProp(cur,(const xmlChar*) "id");

            value = xmlGetProp(cur,(const xmlChar*) "value");

            printf("uri: %s,  value:%s\n", uri,value);

            xmlFree(uri);

        }

        cur = cur->next;

    }

    return;

}

void parseDoc(char *docname) {
    xmlDocPtr doc;

    xmlNodePtr cur;

    doc = xmlParseFile(docname);

    
if (doc == NULL ) {
        fprintf(stderr,"Document not parsed successfully. \n");

        return;

    }

    cur = xmlDocGetRootElement(doc);

    
if (cur == NULL) {
        fprintf(stderr,"empty document\n");

        xmlFreeDoc(doc);

        return;

    }

    
if (xmlStrcmp(cur->name, (const xmlChar *) "settings")) {
        
//fprintf(stderr,"document  of  the  wrong  type,  root  node  !=  story",  xmlFreeDoc(doc));
        printf("root error\n");

        return;

    }

    getReference (doc, cur);

    xmlFreeDoc(doc);

    return;

  }


  
int main(int argc, char **argv) {
    
char *docname;
    
if (argc <= 1) {
        printf("Usage: %s docname\n", argv[0]);

        return(0);

    }

    docname = argv[1];

    parseDoc (docname);

    return (1);

        }


Latest topics
C - pointers to pointers  Viewed: 263
Malloc Debugger  Viewed: 263
C - switch statement  Viewed: 264
C - binary tree  Viewed: 264
Is a prime number or not  Viewed: 264
C - Do while  Viewed: 265
C - Global variables , static variables  Viewed: 265
C - binsearch  Viewed: 265
C Assign content to a function pointer  Viewed: 265
C - Structure example  Viewed: 266

Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
C - Data Structure,Rev Linked list
C - Data Structure,linked list
C - time_t strftime example
C - time_t gmtime example
C - enum example
C - strtok example, Splitting string into tokens
C - libxml2 parsing example libxml
C - if else example
C - Structure example
C - pointers and arrays example strlen
C - Pointers to structures
C - gtk scrolled window example
C - Binary Search Tree example
C TCP socket example Server and Client program
C UDP socket example Server and Client program

Related References
c - basics of structure
c - functions and program structure, programmed pass-by-reference via pointers, pass-by-reference parameters
c - structures and functions
c - arrays of structures
c - pointers to structures
explain function pointers with examples?

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us