VTD-XML: The Future of XML Processing

SourceForge.net Logo

Sourceforge Home

Mailing Lists

XimpleWare

Download


VTD-XML Home

 

A SOAP Processor 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 SOAP message using the C version of VTD-XML. The goal is to pull out all the elements in the SOAP header that has the "mustUnderstand" attribute and write the elements into a new file. The corresponding XML file and the C source file can be downloaded using the links below:

soap2.xml

soap.c (Using XPath)

The following header files are imported:

#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"

The first thing: Set up the global exception context.

_thread struct exception_context the_exception_context[1];

In C, variables need to be declared before any application code.

exception e;
FILE *f = NULL;
FILE *fo = NULL;
int i = 0;

Long l = 0;
int len = 0;
int offset = 0;

char* filename = "./soap2.xml";
struct stat s;
UByte *xml = NULL;
 // This is the buffer containing the XML content, UByte means unsigned byte
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;
UByte *sm = "\n================\n";

The following code opens the input and output file, reads the input file content into a byte 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");
fo = fopen("./out.txt","w");

stat(filename,&s);

i = (int) s.st_size;
printf("size of thefile is %d \n",i);
xml = (UByte *)malloc(sizeof(UByte) *i);
fread(xml,sizeof(UByte),i,f);

Next, the code creates an instance of VTDGen, and parses the XML input.

vg = createVTDGen();
setDoc(vg,xml,i);
parse(vg,TRUE);

Afterwards, the code compiles and evaluates the XPath expression and pull out all element fragments containing the "mustUnderstand" attribute and write them into "out.txt."

vn = getNav(vg);
ap = createAutoPilot2(); 
// the argument constructor for AutoPilot
declareXPathNameSpace(ap,L"ns1",L"http://www.w3.org/2003/05/soap-envelope");
if (selectXPath(ap,L"/ns1:Envelope/ns1:Header/*[@ns1:mustUnderstand]")){
         setVTDNav(ap,vn);
         while(evalXPath(ap)!= -1){
              l = getElementFragment(vn);
// l is a 64 bit integer
              offset = (int) l; 
//lower 32 bits are offset
              len = (int) (l>>32);
  // upper 32 bits are length
              fwrite((char *)(xml+offset),sizeof(UByte),len,fo);
              fwrite((char *) sm,sizeof(UByte),strlen(sm),fo);
         }

}


As the last step, this code example closes both the input and output, the manually de-allocates all data structures.
 

fclose(f);
fclose(fo);

// remember C has no automatic garbage collector
// needs to deallocate manually.
freeVTDNav(vn);
freeVTDGen(vg);
freeAutoPilot(ap);

VTD in 30 seconds

VTD+XML Format

User's Guide

Developer's Guide

VTD: A Technical Perspective

Code Samples

  RSS Reader in Java

  RSS Reader in C

  SOAP in Java

  SOAP in C

  BioInfo in Java

  BioInfo in C

  Modify XML In Java

  Modify XML In C

  Shuffle

  Edit XML

  Index Creation and Loading

  Process Huge XML Files (>2G)

FAQ

Getting Involved

Articles and Presentations

Benchmark

API Doc

Demo