December 18, 2014
Hot Topics:

VTD-XML: A New Vision of XML

  • December 3, 2007
  • By Victor Volkman
  • Send Email »
  • More Articles »

Inserting an Attribute

Of course, to do anything really useful in XML, you need more than the ability to simply read XML. As mentioned in the intro, VTD-XML can splice, dice, insert, and even build templates. Because space is limited here, you'll just push into one more example of inserting an attribute to an input XML file and dumping out an output XML file. Basically, your goal is to transform input.xml

<a >
<b> hello world! </b>
</a>

into new.xml

<a >
<b attr1='val'> hello world! </b>
</a>

Just to make things interesting, you're going to work with a pre-indexed XML file, which you will denote as .VXL (think "VTD-XML"). The advantage of indexing a file is that you save tons of time by only parsing the input file just once in scenarios that might involve multiple passes through the input.

 1 #include "everything.h"
 2 struct exception_context the_exception_context[1];
 3
 4 int makeIndexedFile(char *inputName, char *outputName){
 5    exception e;
 6    VTDGen *vg = NULL;
 7    Try{
 8       vg = createVTDGen();
 9       if (parseFile(vg, TRUE, inputName)){
10           writeIndex2(vg, outputName);
11       }
12       free(vg->XMLDoc);
13    }Catch(e){
14    }
15    freeVTDGen(vg);
16 }
17
18 int main(){
19    exception e;
20    VTDGen *vg = NULL;
21    VTDNav *vn = NULL;
22    AutoPilot *ap = NULL;
23    XMLModifier *xm = NULL;
24    FILE *f = NULL;
25    UCSChar *string = NULL;
26    int i;
27
28    makeIndexedFile("input.xml", "output.vxl");
29    if ((f = fopen("output.vxl","rb")) == NULL)
30       return 0;
31    Try{
32       xm = createXMLModifier();
33       ap = createAutoPilot2();
34       selectXPath(ap,L"/a/b");
35       vg = createVTDGen();
36       vn = loadIndex (vg,f);
37       bind(ap,vn);
38       bind4XMLModifier(xm,vn);
39       while((i=evalXPath(ap))!=-1){
40          insertAttribute(xm,L" attr1='val'");
41       }
42       output2(xm,"new.xml");
43       free(vn->XMLDoc);
44    }Catch(e){    // handle various types of exceptions here
45    }
46    fclose(f);
47    freeAutoPilot(ap);
48    freeXMLModifier(xm);
49    freeVTDGen(vg);
50    freeVTDNav(vn);
51    return 0;
52 }

Briefly, you convert input.xml into a VTD indexed XML file in the makeIndexedFile() function up top. Later, in Line 29, you can just open it with a plain old filehandle and begin using it with just a single call to loadIndex() in Line 36. The interesting parts are where you set up an autopilot to traverse the XML input via an XPath created by selectXPath() in Line 34. Finally, you are able to call insertAttribute() each time evalXPath() gives you a hit and then, when you are done, you just call output2() to dump out the newly transformed XML file.

Conclusion

My hope is that you will be sufficiently convinced of its ease of use to try more. I confess that VTD-XML has been one of the easier libraries to build and use of many I have tried lately. I am indebted to Jimmy Zhang for his assistance in providing all the examples of this article.

About the Author

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an e-mail to sysop@HAL9K.com.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel