The C VOTable Parser Library.

[Introduction]- [Download]- [Tutorial]- [Reference]

Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

Tutorial : Getting Started

Assume you need to read the following VOTable file :
<?xml version="1.0"?>
<VOTABLE version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://www.ivoa.net/xml/VOTable/VOTable/v1.1">
  <DESCRIPTION>Data to import to skywatcher</DESCRIPTION>
  <RESOURCE>
    <PARAM name="Instrument" datatype="char" arraysize="*" value="TOTO">
      <DESCRIPTION>
        This parameter is designed to store intrument's name
      </DESCRIPTION>
    </PARAM>
    <TABLE name="SpectroLog">
      <FIELD name="Target" ucd="meta.id" datatype="char" arraysize="30*"/>
      <FIELD name="Instr" ucd="instr.setup" datatype="char" arraysize="5*"/>
      <FIELD name="Dur" ucd="time.expo" datatype="int" width="5" unit="s"/>
      <FIELD name="Spectrum" ucd="meta.ref.url" datatype="float" arraysize="*"
             unit="mW/m2/nm" type="location">
        <DESCRIPTION>Spectrum absolutely calibrated</DESCRIPTION>
        <LINK type="location"
              href="http://ivoa.spectr/server?obsno="/>
      </FIELD>
      <DATA><TABLEDATA>
        <TR><TD>NGC6543</TD><TD>SWS06</TD><TD>2028</TD><TD>01301903</TD></TR>
        <TR><TD>NGC6543</TD><TD>SWS07</TD><TD>2544</TD><TD>01302004</TD></TR>
      </TABLEDATA></DATA>
    </TABLE>
  </RESOURCE>
</VOTABLE>

The code below that, uses the libVOTable library, does the task :

#include "votable.h"

int main() {
  xmlTextReaderPtr reader;
  list_field *vfield_move;
  list_tabledata *vtabledata_move;
  VOTable votable;
  int nbFields;
  int *columns;
  char file[50]="votable.xml";
  reader = Init_VO_Parser(file,&votable);

  Extract_Att_VO_Table(reader,&votable);
  printf("Table Attribute=%s\n\n",votable.table->name);

  Extract_VO_Fields(reader,&votable,&nbFields,&columns);
  for(vfield_move=votable.field;vfield_move!=NULL;vfield_move=vfield_move->next) {
    printf("name=%s\nucd=%s\ndatatype=%s\narraysize=%s\ntype=%s\nwidth=%s\nunit=%s\n\n",
           vfield_move->name,
           vfield_move->ucd,
           vfield_move->datatype,
           vfield_move->arraysize,
           vfield_move->type,
           vfield_move->width,
           vfield_move->unit);
    if(xmlStrcmp(vfield_move->ucd,"meta.id") == 0) 
      columns[0] = vfield_move->position;
    if(xmlStrcmp(vfield_move->ucd,"meta.ref.url") == 0) 
      columns[1] = vfield_move->position;
 }


 Extract_VO_TableData(reader,&votable, nbFields, columns);
 for(vtabledata_move=votable.tabledata;vtabledata_move!=NULL;vtabledata_move=vtabledata_move->next) {  
   printf("All values=%s\n",vtabledata_move->value);
   if (vtabledata_move->colomn == columns[0])
     printf("ucd=meta.id value=%s\n",vtabledata_move->value);
   if (vtabledata_move->colomn == columns[1])
     printf("ucd=meta.ref.url value=%s\n",vtabledata_move->value);
 }

  if (Free_VO_Parser(reader,&votable,&columns) == 1)
    fprintf(stderr,"memory problem\n");
  return 0;
}

The program output is :

Table Attribute=SpectroLog

name=Spectrum
ucd=meta.ref.url
datatype=float
arraysize=*
type=location
width=(null)
unit=mW/m2/nm

name=Dur
ucd=time.expo
datatype=int
arraysize=(null)
type=(null)
width=5
unit=s

name=Instr
ucd=instr.setup
datatype=char
arraysize=5*
type=(null)
width=(null)
unit=(null)

name=Target
ucd=meta.id
datatype=char
arraysize=30*
type=(null)
width=(null)
unit=(null)

All values=01302004
ucd=meta.ref.url value=01302004
All values=NGC6543
ucd=meta.id value=NGC6543
All values=01301903
ucd=meta.ref.url value=01301903
All values=NGC6543
ucd=meta.id value=NGC6543

Detailled description of each task, line by line :

#include "votable.h"
Include the main and only header file of the libVOTable library.
int main(){
Definition of the main function.
xmlTextReaderPtr reader;
the xmlTextReaderPtr used
list_field *vfield_move;
Pointer on FIELD datatype
list_tabledata *vtabledata_move;
Pointer on TABLEDATA datatype
VOTable votable;
VOTABLE structure declaration
int nbFields;
Number of FIELD
int *columns;
Integer array of TD column number to parse
char file[50]="votable.xml";
Filename to parse
reader = Init_VO_Parser(file,&votable);
Memory and structure initialization
Extract_Att_VO_Table(reader,&votable);
Extract TABLE attributes
printf("Table Attribute=%s\n\n",votable.table->name);
Display result
Extract_VO_Fields(reader,&votable,&nbFields,&columns);
Extract FIELD attributes
for(vfield_move=votable.field;vfield_move!=NULL;vfield_move=vfield_move->next) {
  printf("name=%s\nucd=%s\ndatatype=%s\narraysize=%s\ntype=%s\nwidth=%s\nunit=%s\n\n",
          vfield_move->name,
          vfield_move->ucd,
          vfield_move->datatype,
          vfield_move->arraysize,
          vfield_move->type,
          vfield_move->width,
          vfield_move->unit);
  if(xmlStrcmp(vfield_move->ucd,"meta.id") == 0) 
      columns[0] = vfield_move->position;
  if(xmlStrcmp(vfield_move->ucd,"meta.ref.url") == 0) 
      columns[1] = vfield_move->position;
}
Browse the FIELD linking list and prepare the parsing for data for which ucd are meta.id and meta.ref.url
Extract_VO_TableData(reader,&votable, nbFields, columns);
Extract data for which ucd are meta.id and meta.ref.url
for(vtabledata_move=votable.tabledata;vtabledata_move!=NULL;vtabledata_move=vtabledata_move->next) {  
  printf("All values=%s\n",vtabledata_move->value);
  if (vtabledata_move->colomn == columns[0])
    printf("ucd=meta.id value=%s\n",vtabledata_move->value);
  if (vtabledata_move->colomn == columns[1])
    printf("ucd=meta.ref.url value=%s\n",vtabledata_move->value);
}
Browse the TABLEDATA linking list and display parsing result
if (Free_VO_Parser(reader,&votable,&columns) == 1)
   fprintf(stderr,"memory problem\n");
Free parser memory