Created
January 25, 2014 01:44
-
-
Save mygnu/8610500 to your computer and use it in GitHub Desktop.
xmlparsig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Code: */ | |
#include <stdio.h> | |
#include <string.h> | |
#include "internodeAPI.h" | |
#include "xmlparsing.h" | |
#define BASEXML "tmp/nodeservice.xml" | |
/* sample XML file */ | |
/* <internode> */ | |
/* <api> */ | |
/* <services count="1"> */ | |
/* <service type="Personal_ADSL" href="/api/v1.5/ID">ID_changed</service> */ | |
/* </services> */ | |
/* </api> */ | |
/* </internode> */ | |
#define USAGEXML "tmp/nodeusage.xml" | |
#define HISTORYXML "tmp/nodehistory.xml" | |
/* sample XML file */ | |
/* <internode><api> */ | |
/* <service type="Personal_ADSL" request="history">ID_changed</service> */ | |
/* <usagelist> */ | |
/* <usage day="2013-01-25"> */ | |
/* <traffic name="total" unit="bytes">2084408661</traffic> */ | |
/* </usage> */ | |
/* <usage day="2013-01-26"> */ | |
/* <traffic name="total" unit="bytes">3038963621</traffic> */ | |
/* </usage> */ | |
/* </usagelist> */ | |
/* </api></internode> */ | |
int | |
main(int argc, char *argv[]) | |
{ | |
char baseurl[80] = "https://customer-webtools-api.internode.on.net/api/v1.5/"; | |
/* get the first xml for the internode userid */ | |
getNodeXml(baseurl, BASEXML); // this function takes url and saves the xml file to the path given using libcurl defined in internodeAPI.h | |
char * serviceID = getElementContent(BASEXML, "service"); | |
printf("%s\n", serviceID ); // i get ID_changed printed | |
strcat(baseurl, serviceID); | |
char history[80]; | |
strcpy(history, baseurl); | |
strcat(history,"/history/"); | |
char usage[80]; | |
strcpy(usage, baseurl); | |
strcat(usage,"/usage/"); | |
printf("%s\n",baseurl ); /* all of these work */ | |
printf("%s\n",history ); | |
printf("%s\n",usage ); | |
getNodeXml(usage, USAGEXML); | |
getNodeXml(history, HISTORYXML); | |
getNodeXml(baseurl, HISTORYXML); | |
char * traffic = getElementContent(HISTORYXML, "traffic"); | |
printf("%s\n", traffic); /* segmentation fault */ | |
return 0; | |
} | |
/* main.c ends here */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Code: */ | |
#include "xmlparsing.h" | |
#include <libxml/parser.h> | |
#include <libxml/xpath.h> | |
#include <stdio.h> | |
#include <string.h> | |
static xmlDocPtr | |
getdoc (const char *filename); | |
static xmlXPathObjectPtr | |
getnodeset (xmlDocPtr docPtr, xmlChar *xpath); | |
/* takes two pointers to string as name of the document and element */ | |
char * | |
getElementContent(char * filename, char * elementname) | |
{ | |
char element[50] = "//"; /* needs // prefix for some reason */ | |
strcat(element, elementname); | |
xmlDocPtr doc = getdoc(filename); /* xmlDocPtr to the document */ | |
xmlChar *xpath = (xmlChar*) element; /* is the name of the element */ | |
xmlXPathObjectPtr result = getnodeset (doc, xpath); | |
xmlNodeSetPtr nodeset; | |
int i; | |
xmlChar *keyword = (xmlChar *) malloc(sizeof(char) * 50); // not sure how to do this properly | |
if (result) /* if not NULL */ | |
{ | |
nodeset = result->nodesetval; | |
for (i=0; i < nodeset->nodeNr; i++) | |
{ | |
keyword = xmlNodeListGetString | |
(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1); | |
return (char *)keyword; | |
xmlFree(keyword); | |
} | |
xmlXPathFreeObject (result); | |
} | |
xmlFreeDoc(doc); | |
xmlCleanupParser(); | |
return NULL; | |
} | |
/* takes pointer to a string of the name of the document */ | |
static xmlDocPtr | |
getdoc (const char *filename) | |
{ | |
xmlDocPtr docPtr; | |
docPtr = xmlParseFile(filename); | |
if (docPtr == NULL ) { | |
fprintf(stderr,"Document not parsed successfully. \n"); | |
return NULL; | |
} | |
return docPtr; | |
} | |
/* takes a xmlDocPtr to the xml document and a xmlChar string pointer as | |
an element name */ | |
static xmlXPathObjectPtr | |
getnodeset (xmlDocPtr doc, xmlChar *xpath) | |
{ | |
xmlXPathContextPtr context; | |
xmlXPathObjectPtr result; | |
context = xmlXPathNewContext(doc); /* initialize the context */ | |
if (context == NULL) /* test if null */ | |
{ | |
printf("Error in xmlXPathNewContext\n"); | |
return NULL; | |
} | |
result = xmlXPathEvalExpression(xpath, context); /* evaluates XPath location | |
in the given context */ | |
xmlXPathFreeContext(context); /* free the context memory */ | |
if (result == NULL) | |
{ | |
printf("Error in xmlXPathEvalExpression\n"); | |
return NULL; | |
} | |
if(xmlXPathNodeSetIsEmpty(result->nodesetval)) | |
{ | |
xmlXPathFreeObject(result); | |
printf("There is no element of this name \n"); | |
return NULL; | |
} | |
return result; | |
} | |
/* xmlparsing.c ends here */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment