Archive for March, 2011|Monthly archive page

VTD-XML 2.10 Released

VTD-XML 2.10 is now released under Java, C#, C and C++. It can be downloaded at This release includes a number of new features and enhancement.

  • The core API of VTD-XML has been expanded. Users can now perform cut/paste/insert on an empty element.
  • This release also adds the support of deeper location cache support for parsing and indexing. This feature is useful for application performance  tuning for processing various XML documents.
  • The java version also added support for processing zip and gzip files. Direct processing of httpURL based XML is enhanced.
  • Extended Java version now support Iso-8859-10~16 encoding.
  • A full featured C++ port is released.
  • C version of VTD-XML now make use of thread local storage to address the  thread-safety issue for multi-threaded application.

There are also a number of bugs fixed. Special thanks to Jozef Aerts, John Sillers, Chris Tornau and a number of other users for input and suggestions

Thread Safety in C Version of VTD-XML

Before 2.10, the C version of vtd-xml makes extensive use of global variables for XPath query compilation. The thread safety problem arises when multple instances of an application  perform  XPath compilation at the same time. To resolve this issue, VTD-XML 2.10 replaces all global variables for XPath compilation with thread local vriables: instead of simply declaring a variable, prepend _thread to the declaration. The thread local variable is just like global variable, except it is specific/visible within a thread. The macro for “_thread” is defined in “customTypes.h.”

How does the use of thread local variable impact the overall design of your application? Fortunately, very little change is required. The most significant one is the global thread context declaration: The old one looks something like this:

struct exception_context the_exception_context[1];
int main(){
          exception e;
          Try {  // put the code throwing exceptions here
          } Catch (e) {  // handle exception in here

From 2.10 and onward the app will look like 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

Location Cache Depth Tuning

Before version 2.10, the location cache depth is set to 3. In this version, you can choose either 3 or 5, by simply calling VTDGen’s setLcDepth() (see the example below). The benefit is that at the cost of negligible parsing and memory overhead, the random access performance of VTDNav improves, especially for depth XML documents.

   VTDGen vg = new VTDGen();


Insert Text into Empty Element

In this release,  you now have the ability to insert text into an empty element  (e.g. <a/>).

  • Insert “some text” into <a/>, you get “<a>some text</a>”.
  • Insert <b/> into <a/>, you get <a><b/></a>. 

VTD-XML in  C++

Below is a simple app written in VTD-XML and C++.

#include "everything.h"
//#include "bookMark.h"

using namespace com_ximpleware;

int main(){
 FILE *f = NULL;
 FILE *fo = NULL;
 int i = 0;

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

 char* filename = "c:/xml/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;
 char *sm = "\n================\n";

 // 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("c:/xml/out.txt","w");


 i = (int) s.st_size; 
 printf("size of the file is %d \n",i);
 xml = new UByte[i];
 VTDGen vg;
  vn = vg.getNav();
  AutoPilot ap;
  ap.declareXPathNameSpace(L"ns1",L"<a href=""></a>");
  //if (ap.selectXPath(L"/ns1:Envelope/ns1:Header/*[@ns1:mustUnderstand]")){
  if (ap.selectXPath(L"/ns1:Envelope/ns1:Header/*[@ns1:mustUnderstand]")){
  //if (ap.selectXPath(L"/a/b/*")){
   int i=-1;
   while((i=ap.evalXPath())!= -1){
    //printf("\n hi ==> %d \n",i);
    l = vn->getElementFragment();
    offset = (int) l;
    len = (int) (l>>32);
    fwrite((char *)(xml+offset),sizeof(UByte),len,fo);
    fwrite((char *) sm,sizeof(UByte),strlen((char*)sm),fo);
  // remember C has no automatic garbage collector
  // needs to deallocate manually.
 catch (ParseException &e){
  printf(" error ===> %s \n",e.getMessage());
 catch (...) {
  delete (vn);
 return 0;