Example 3 - XML Test-file Generator


/* -------------------------------------------------------------------------
 Test_xml_generator.c - A simple example of constructing arbitrary
   xml-file using re-usable xml-library.

  Create new XML file by using:
   xml_tree_init();
   xml_tree_add_tag( xml_tree, tagname );
   xml_tree_add_contents( xml_tree, contents );
   xml_tree_add_attribute( xml_tree, name, value );
   xml_tree_begin_children( xml_tree );
   xml_tree_end_children( xml_tree );
   xml_tree_cleanup( xml_tree );
   Xml_Write_File( filename, xml_tree );

 Compile:
  cc -g test_xml_generator.c -lm -o test_xml_generator.exe
 ---------------------------------------------------------------------------
*/

#include <stdio.h>
#include "xml_parse_lib.c"


int main( int argc, char **argv )
{
 int j=1;
 Xml_object *rootobj;
 struct xml_private_tree *xml_tree;

 xml_tree = xml_tree_init();	/* Initialize the tree. */

 /* Now add some objects, attributes, etc... */

 xml_tree_add_tag( xml_tree, "first_tag" );
 xml_tree_add_attribute( xml_tree, "attrib1", "value1" );
 xml_tree_add_attribute( xml_tree, "attrib2", "value2 <> &" );
 xml_tree_add_contents( xml_tree, "Some contents 1 <> &" );

 xml_tree_begin_children( xml_tree );
  xml_tree_add_tag( xml_tree, "first_child" );
  xml_tree_add_contents( xml_tree, "Some contents 11" );
  xml_tree_add_tag( xml_tree, "second_child" );
  xml_tree_add_contents( xml_tree, "Some contents 22" );
  xml_tree_add_attribute( xml_tree, "attrib21", "value21" );
  xml_tree_add_attribute( xml_tree, "attrib22", "value22" );

  xml_tree_begin_children( xml_tree );
   xml_tree_add_tag( xml_tree, "first_grand_child" );
   xml_tree_add_contents( xml_tree, "Some contents 33" );
   xml_tree_add_attribute( xml_tree, "attrib31", "value31" );

   xml_tree_add_tag( xml_tree, "second_grand_child" );
   xml_tree_add_contents( xml_tree, "Some contents 44" );
   xml_tree_add_attribute( xml_tree, "attrib41", "value41" );
  xml_tree_end_children( xml_tree );

 xml_tree_end_children( xml_tree );

 xml_tree_add_tag( xml_tree, "second_tag" );
 xml_tree_add_contents( xml_tree, "Some contents 2" );

 rootobj = xml_tree_cleanup( &xml_tree );

 Xml_Write_File( "test_out.xml", rootobj );

 printf("\nWrote 'test_out.xml'.\n");
 return 0;
}


Back

SourceForge.net Logo