Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Database

XML, SQL, and C


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

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.