VTD-XML: The Future of XML Processing

SourceForge.net Logo

Sourceforge Home

Mailing Lists

XimpleWare

Download


VTD-XML Home

 

Incrementally Modify XML 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 application shows you how to incrementally update XML documents using C version of VTD-XML's XMLModifier. The C code and the XML file are shown below:

ModifyXML.c

old.xml

The input and output XML are shown below:

old.xml

new.xml

<document attr="">
<a> old content </a>
</document>

<document attr="value">
<b/>
<a> new content </a>
<c/>
</document>

 

The application logic goes into the following code block:

#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"
#include "XMLModifier.h"


//set up the global exception context
struct exception_context the_exception_context[1];

int main(){
     exception e;
     VTDGen *vg = NULL;
     VTDNav *vn = NULL;
     AutoPilot *ap = NULL;
     XMLModifier *xm = NULL;
     int i,j,k;

     Try{

           // application logic goes here !!!

 

     }Catch(e){
     }
     freeVTDGen(vg);

     freeVTDNav(vn);
     freeXMLModifier(xm);
     return 0;

}

 

 

The application logic (along with comments) is shown below:

vg = createVTDGen();
if (parseFile(vg,TRUE,"old.xml")){
     vn = getNav(vg);
     xm = createXMLModifier();
     bind4XMLModifier(xm,vn);

         // first update the value of attr
     i = getAttrVal(vn,L"attr");
     if (i!=-1){
       updateToken(xm,i,L"value");
     }

        // navigate to <a>
     if (toElement2(vn, FIRST_CHILD, L"a")) {
      
  // update the text content of <a>
        i=getText(vn);
        if (i!=-1){
            updateToken(xm,i, L" new content ");
        }
     
    // insert an element before <a> (which is the cursor element)
        insertBeforeElement(xm, L"<b/>\n\t");
               // insert an element after <a> (which is the cursor element)
        insertAfterElement(xm,  L"\n\t<c/>");
     }
     output2(xm,"new.xml");
     free(vn->XMLDoc);
// free the XML byte buffer
}

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

  Overwrite XML

  Index Creation and Loading

  Process Huge XML Files (>2G)

FAQ

Getting Involved

Articles and Presentations

Benchmark

API Doc

Demo