A RSS
Reader in C
(For more code samples,
visit Official VTD-XML Blog)
(Separate code-only
VTD-XML tutorials are available in
C,
C++,
Java
and
C#)
This example shows how to process a
RSS news feed using C version of VTD-XML. The corresponding XML file and C
files can be downloaded using the links below:
servers.xml
RSSReader.c (Using
XPath)
The following header files are included:
#include <string.h>
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "xpath1.h"
#include "helper.h"
#include "vtdGen.h" |
Since the C version of VTD-XML uses
a catch/throw exception mechanism similar to Java, the first thing is to
set up the exception context. Since v2.10, prepend "_thread" to the exception
context. Also the template for doing try and catch
are shown below:
_thread struct exception_context
the_exception_context[1]; int
main(){
exception e;
Try { // put the code throwing
exceptions here
} Catch (e) { // handle exception
in here
}
} |
The C syntax mandates that all
variables be declared at the beginning, so the code below declares those
variables at the start of main function.
FILE *f = NULL;
int i = 0,t,result,count=0;
wchar_t *tmpString;
char* filename = "./servers.xml";
struct stat s;
UByte *xml = NULL; // this is the buffer containing the XML
content,
VTDGen *vg = NULL; // This is the VTDGen that parses XML
VTDNav *vn = NULL; // This is the VTDNav that navigates the VTD
records
AutoPilot *ap = NULL; |
The next step is to read the raw
bytes of servers.xml into a memory buffer.
// allocate a piece of buffer
then reads in the document content
// assume "c:\soap2.xml" is the name of the file
f = fopen(filename,"r");
stat(filename,&s);
i = (int) s.st_size;
wprintf(L"size of the file is %d \n",i);
xml = (UByte *)malloc(sizeof(UByte) *i);
i = fread(xml,sizeof(UByte),i,f); |
Then, parse the byte buffer using the
"parse(...)" method of VTDGen.
vg = createVTDGen();
setDoc(vg,xml, i);
parse(vg,TRUE); |
In this
step, retrieve the VTDNav object from VTDGen and instantiate the AutoPilot
object.
vn = getNav(vg);
ap = createAutoPilot2();
declareXPathNameSpace(ap,L"ns1",L"http://purl.org/dc/elements/1.1/"); |
The code using the XPath feature of VTD-XML version
1.0 is shown below. The XPath expression is compiled by calling "selectXPath()"
of AutoPilot. Calling evalXPath() moves the cursor to the
selected nodes in the node set. It is important to notice that to denote a
Unicode string, you must prepend "L" to the string literal.
if (selectXPath(ap,L"//ns1:*")){
setVTDNav(ap,vn);
while((result=evalXPath(ap))!= -1){
wprintf(L"result is %d \n",result);
tmpString = toString(vn,result);
wprintf(L"Element name ==> %ls \n",tmpString);
free(tmpString);
t = getText(vn);
if (t!=-1){
tmpString = toNormalizedString(vn,t);
wprintf(L" text ==> %ls \n",tmpString);
free(tmpString);
}
wprintf(L"\n =======================\n ");
count ++;
}
}
wprintf(L"\nTotal number of elements %d \n",count);
fclose(f); |
Since C doens't garbage collect automatically, we
must call free out-of-scope variables explicitly.
freeVTDNav(vn);
freeVTDGen(vg);
freeAutoPilot(ap); |