FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Database
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
October 30, 2006

XML, SQL, and C

(Page 2 of 6)

Using autoXml

Listing One is an XML document that describes a couple of geometrical shapes. You could use autoXml to generate a module called "poly" that handles reading/writing this type of XML. Listing Two is a routine that uses the poly module. This routine reads-in the XML, shrinks all the shapes by 50 percent, and writes the results to a new XML file.

<SHAPES>
  <POLYGON color="red" name="triangle">
    <DESCRIPTION>A 3-4-5 right triangle</DESCRIPTION>
    <PT-TWO-D x="0" y="0" />
    <PT-TWO-D x="3.0" y="0" />
    <PT-TWO-D x="0" y="4" />
  </POLYGON>
  <POLYGON color="black" name="square">
    <DESCRIPTION>Your basic black unit square</DESCRIPTION>
    <PT-TWO-D x="0" y="0" />
    <PT-TWO-D x="1" y="0" />
    <PT-TWO-D x="1" y="1" />
    <PT-TWO-D x="0" y="1" />
  </POLYGON>
</SHAPES>
Listing One

#include "poly.h"
void shrinkShape(char *in, char *out)
{
struct polyShapes *shapes = polyShapesLoad(in);
struct polyPolygon *polygon;
FILE *f;

/* Loop through all points of all polygons, scaling by 50% */ for (polygon = shapes->polyPolygon; polygon != NULL; polygon = polygon->next { struct polyPtTwoD *point; for (point = polygon->polyPoint; point != NULL; point = point->next) { point->x *= 0.5; point->y *= 0.5; } printf("Shrunk %s\n", polygon->description->text); }

/* Save result. */ f = fopen(out, "w"); polyShapesSave(shapes, 0, f); fclose(f); }

Listing Two

Because XML allows dashes and other characters in its identifiers that are illegal in C, some mapping of names is required when going between C and XML. AutoXml converts the identifiers to a mixed-case style, where capital letters are used to separate words within an identifier. Thus PT-TWO-D becomes ptTwoD. AutoXml also prepends a prefix to each function and struct name. By default, the prefix is the same as the module name. Because field names are not in a global namespace in C, no prefix is applied there. The text between tags is available in a field named "text." In case of name conflicts, you can name it something else with the -textField command-line option.

In Listing Two, the entire XML file is loaded into memory before processing. In my work, though, I frequently encounter XML files that won't all fit in memory. For instance, XML dumps of the National Center for Biotechnology Information's dbSNP database of human genetic variations is over 11 GB after compression with gzip! Listing Three shows how to process the sample XML file one polygon at a time.

void processPolysInLimitedMemory(char *in)
{
struct xap *xap = xapOpen(in);
struct polyPolygon *polygon;

while ((polygon = polyPolygonNext(xap)) != NULL) { /* Do some processing to polygon here... */ polyPolygonFree(&polygon); } xapFree(&xap); }

Listing Three
Previous Page | 1 XML, SQL, and C | 2 Using autoXml | 3 Using autoDtd | 4 Using sqlToXml | 5 Using xmlToSql | 6 Inside the Programs Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK