2

I just want to create a XML file (I have XML schema for the file) at a given location, and write a structure into it.

For instance

struct my_data
{
int no;
char *input ;
char *output;
char * descritpiton;
char *time;
};

"Expat" and "Xerces" are two options but i dont want any parsing stuff(As these libs are basically xml parsers). So,to just create one xml file , i dont think,these options are an efficient way.

Any Help ??

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
mujahid
  • 691
  • 2
  • 12
  • 21

4 Answers4

3

XML is text - one option is simply emitting the needed document. There are problems (you must make sure to escape entities, etc), but its the zero-overhead approach.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
2

XML libraries usually can do both parsing and generation.

If your XML files are simple enough, you could output them directly with e.g. fprintf. But then you probably need a routine to escape XML characters appropriately (< as &lt; etc...).

I would recommend still using some light XML library.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

XML is just ASCII. However you need to encode the strings - which is a simple process to implement. See http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references for the encodings that are required.

This code will do this:

char *s = "some string that needs encoed &<>"'";
for (; *s; ++s)
{
   switch (*s)
   {
      case '&': printf("&amp;"); break;
      case '<': printf("&lt;"); break;
      case '>': printf("&gt;"); break;
      case '"': printf("&quot;"); break;
      case '\'': printf("&apos;"); break;
      default: putc(*s, stdout); break;
   }
}
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
2

well mostly i use mxml library in such condition but here you just want to make xml file dont want to parse that then you can directly make that xml file just like this way

#include<stdio.h>

struct my_data
{
int number;
char string[10];
};

void createdata(FILE *fb,struct my_data testData)
{
fprintf ( fb,"<Data>\n");
fprintf ( fb,"<number> %d </number>\n",testData.number);
fprintf ( fb,"<string> %s </string>\n",testData.string);
fprintf ( fb,"</Data>\n");
}

int main()
{
FILE *fb=fopen("test.xml","w");
struct my_data testData = {32,"Mr.32"};
fprintf ( fb,"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
createdata(fb,testData);
return 0;
}

this is just ruff code to help you to think... this code make belows xml file

<Data>
<number> 32 </number>
<string> Mr.32 </string>
</Data> 
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222